1

I have a string.

string str = "TTFTTFFTTTTF";

How can I break this string and add character ","?

result should be- TTF,TTF,FTT,TTF
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
Dilee
  • 21
  • 1
  • 4

3 Answers3

2

You could use String.Join after you've grouped by 3-chars:

var groups = str.Select((c, ix) => new { Char = c, Index = ix })
    .GroupBy(x => x.Index / 3)
    .Select(g => String.Concat(g.Select(x => x.Char)));
string result = string.Join(",", groups);

Since you're new to programming. That's a LINQ query so you need to add using System.Linq to the top of your code file.

  • The Select extension method creates an anonymous type containing the char and the index of each char.
  • GroupBy groups them by the result of index / 3 which is an integer division that truncates decimal places. That's why you create groups of three.
  • String.Concat creates a string from the 3 characters.
  • String.Join concatenates them and inserts a comma delimiter between each.
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

You can use insert

Insert places one string into another. This forms a new string in your C# program. We use the string Insert method to place one string in the middle of another one—or at any other position.

Tip 1: We can insert one string at any index into another. IndexOf can return a suitable index.

Tip 2: Insert can be used to concatenate strings. But this is less efficient—concat, as with + is faster.

for(int i=3;i<=str.Length - 1;i+=4)
{
    str=str.Insert(i,",");
}
Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • @maytham-ɯɐɥıλɐɯ - What do u mean by buggy ?? you can have a look on [Insert Function](https://msdn.microsoft.com/en-us/library/system.string.insert(v=vs.110).aspx) – Mohit S Aug 21 '15 at 08:21
  • Another smaller example can be viewed [here](http://stackoverflow.com/a/24199187/3796048) – Mohit S Aug 21 '15 at 08:22
  • 1
    When you insert the first comma @MohitShrivastava you shift all subsequent characters by one, so your next `6` will not be in right position anymore. – GSerg Aug 21 '15 at 08:28
  • @GSerg please have a look. Thanks for the error shown. I havent run it b4 posting. – Mohit S Aug 21 '15 at 08:35
  • 1
    @MohitShrivastava so I think you figure it out what buggy meant ;) lol – Maytham Fahmi Aug 21 '15 at 08:37
  • 1
    That new version is much worse. You can [have a play](https://dotnetfiddle.net/) before posting. – GSerg Aug 21 '15 at 08:38
  • Btw Insert not insert and Length not length. The new code still does not works Have you tried it in c# 2013? – Maytham Fahmi Aug 21 '15 at 08:45
0

Here is a really simple solution using StringBuilder

    var stringBuilder = new StringBuilder();
    for (int i = 0; i < str.Length; i += 3)
    {
            stringBuilder.AppendFormat("{0},", str.Substring(i, 3));
    }
    stringBuilder.Length -= 1;
    str = stringBuilder.ToString();

I'm not sure if the following is better.

    stringBuilder.Append(str.Substring(i, 3)).Append(',');

I would suggest to avoid LINQ in this case as it will perform a lot more operations and this is a fairly simple task.

Bauss
  • 2,767
  • 24
  • 28
  • 1
    This is more efficient, all the more if the string is very large or it's used with many strings. But you need to take care of some edge cases. If the string length is not divisible through 3 this gives currently an error. If the string is empty you get another. – Tim Schmelter Aug 21 '15 at 07:59