0

Right now I take in a string like this "112233 112233 112233 112233", and I split it into an array like this:

string text = ProcessString("112233 112233");
string[] dates = text.Split(' ');

And that works great, but I want to use string builder to build my string so they would end up like 11-22-33 11-22-33 etc.

So I did try this:

static string ProcessString(string input)
{
    StringBuilder buffer = new StringBuilder(input.Length * 3 / 2);

    for (int i = 0; i < input.Length; i++)
    {
        if ((i > 0) & (i % 2 == 0))
            buffer.Append("-");
        buffer.Append(input[i]);
    }
    return buffer.ToString();
}

It works, but it does not match the expected output of:

11-22-33 
11-22-33

My current output is:

11-22-33- 
1-12-23-3
-11-22-33

What can I do to fix this?

ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
Foo Bar
  • 87
  • 2
  • 10
  • If there's only three elements, take it out of the for loop and use string.Format. string.Format("{0}-{1}-{2}", element1, element2, element3) – Ian P Aug 12 '15 at 13:19
  • 6
    Why not split on space first, then insert the dashes? – juharr Aug 12 '15 at 13:21
  • string text = "112233 112233 112233 112233"; string[] dates = text.Split(' ').Select(x => x.Substring(1,2) + "-" + x.Substring(2,2) + "-" + x.Substring(4, 2)).ToArray(); – jdweng Aug 12 '15 at 13:23
  • @jdweng That really should be an answer instead of a comment. – juharr Aug 12 '15 at 13:25
  • @IanP the 3 strings were just there because I wanted to test, I don't want to hard code the values really. – Foo Bar Aug 12 '15 at 13:26

5 Answers5

2

You can process a single string simply by iterating and collecting size-2 substrings of the string, and then joining them by the - character:

string s = "112233";

List<string> parts = new List<string>(s.Length / 2);
for (int i = 0; i < s.Length; i += 2)
    parts.Add(s.Substring(i, 2));

Console.WriteLine(string.Join("-", parts)); // 11-22-33

So, for your full problem, you could do this:

static string ProcessString(string input)
{
    var segments = input.Split(' ').Select(s =>
    {
        List<string> parts = new List<string>(s.Length / 2);
        for (int i = 0; i < s.Length; i += 2)
            parts.Add(s.Substring(i, 2));
        return string.Join("-", parts);
    });

    return string.Join(" ", segments);
}
poke
  • 369,085
  • 72
  • 557
  • 602
1

Can i offer you another Regex + LINQ approach?

var newDates = dates.Select(d => Regex.Replace(d, ".{2}", "$0-").Trim('-'));
string result = string.Join(" ", newDates);

But i like this extension more because it's readable and re-usable:

public static IEnumerable<String> SplitInParts(this String s, Int32 partLength)
{
    if (s == null)
        throw new ArgumentNullException("s");
    if (partLength <= 0)
        throw new ArgumentException("Part length has to be positive.", "partLength");

    for (var i = 0; i < s.Length; i += partLength)
        yield return s.Substring(i, Math.Min(partLength, s.Length - i));
}

Then the code is even easier:

var newDates = dates.Select(d => string.Join("-", d.SplitInParts(2)));
string result = string.Join(" ", newDates);
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

How about regex:

string s = "112233";
string pattern = @"\d{2}\B";
string result = Regex.Replace(s, pattern, m => m.Value + "-");
Alexander Petrov
  • 13,457
  • 2
  • 20
  • 49
0

I have this approach :

       string input = "112233 445566 778899 101010";
       string[] dates = input.Split(' ');

       foreach (string date in dates){
           Console.WriteLine(date);
           string result = date.Substring(0, 2) + '-' + date.Substring(2, 2) + "-" + date.Substring(4, 2);
           Console.WriteLine(result);
       }
Ricardo Rocha
  • 14,612
  • 20
  • 74
  • 130
0

You can use Array.ConvertAll:

string str = "112233 112233 112233 112233";
string[] dates = str.Split();

dates = Array.ConvertAll(dates, s => s.Insert(4, "-").Insert(2, "-"));

foreach (var s in dates)
    Console.WriteLine(s);
w.b
  • 11,026
  • 5
  • 30
  • 49