1

I am trying to make some logic, but it is not ful proof yet:

string s = "1234567877y";
StringBuilder sb = new StringBuilder(s);
for (int i = 5; i <= s.Length + (s.Length / 5 ); i += 5 + (s.Length / 5)  )
{
     sb.Insert(i, Environment.NewLine);
}
Console.WriteLine(sb.ToString());
Console.ReadKey();

Output should be:

12345
67877
y

StringBuilder contain:  "12345\r\n67877\r\ny"

But if i change input string i am not getting desired result. What i can do here or suggest some other way around.

EDIT: if i change

string s = "1234567877y3434";

output will be

12345
67877y
3434
A.T.
  • 24,694
  • 8
  • 47
  • 65

2 Answers2

7

Another concise approach is using GroupBy:

string s = "1234567877y";
IEnumerable<string> groups = s.Select((c, index) => new{ c, index })
    .GroupBy(x => x.index / 5)
    .Select(xg => string.Join("", xg.Select(x => x.c)));

string result = string.Join(Environment.NewLine, groups);

Here is an approach with a StringBuilder which is more efficient:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
    if ((i+1) % 5 == 0)
        sb.Append(s[i]).Append(Environment.NewLine);
    else
        sb.Append(s[i]);
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • @Arun string builder inside possibly – Grundy Nov 19 '13 at 13:19
  • this is fine approach but i am looking for logic using core c# only – A.T. Nov 19 '13 at 13:21
  • @Arun, uh, LINQ is core to the C# language. Saying `core` there, and implying that `StringBuilder` is more core than LINQ is like saying if Microsoft ever added a feature in any subsequent version I don't want to use it. If that's the case, go back to v1.0 and use that. – Mike Perrenoud Nov 19 '13 at 13:24
  • @Arun: I've editd my answer to add the `StringBuilder` approach. Your approach was incorrect because you have initialized the `StringBuilder` which the complete string but instead you have to append char for char. You also have to use `%` operator. – Tim Schmelter Nov 19 '13 at 13:31
  • @TimSchmelter why you use `i++` instead of `i+=5`? – Grundy Nov 19 '13 at 13:33
  • @Grundy: Because i add char for char to the `StringBuilder`. I use the [`%` operator](http://msdn.microsoft.com/en-us/library/0w4e0fzs(v=vs.110).aspx) to check if i need to append also the `Environment.NewLine` – Tim Schmelter Nov 19 '13 at 13:34
  • @TimSchmelter is it more effective than adding a substring? – Grundy Nov 19 '13 at 13:35
  • 1
    @Grundy: `String.Substring` is efficient. However, it is redundant and just makes the code less readable and more error-prone. The array indexer has a `0(1)` complexity, so it doesn't matter how large the string is, you can always access a char efficiently. Also, it is not more efficient to append multiple characters instead of one each time you call `StringBuilder.Append`. – Tim Schmelter Nov 19 '13 at 13:40
  • I like the % Modulo operator. Thanks – A.T. Nov 20 '13 at 04:56
2

i would do it this way :)

        string s = "1234567877y3434";

        for (int i = 5; i < s.Length; i += 5 + Environment.NewLine.Length)
        {
            s = s.Substring(0, i) + Environment.NewLine + s.Substring(i, s.Length - i);
        }

EDIT: i corrected my code using the suggstestion from Chris using Environment.NewLine instead of \r\n

Stefan
  • 528
  • 4
  • 12
  • 1
    Yup. The original `i += 5 + (s.Length / 5) ` was just strange if the intent was five characters per line. Though I would have stuck with using Environment.NewLine. You could always do `i+=5+Environment.NewLine.Length` as well to make it much clearer what you are doing. – Chris Nov 19 '13 at 13:28