2

I have a string like this

abcdefghij

And I wast to split this string by 3 characters each. My desired output will be a string array containing this

abc
def
ghi
j

Is is possible using string.Split() method?

dcastro
  • 66,540
  • 21
  • 145
  • 155
None
  • 5,582
  • 21
  • 85
  • 170

5 Answers5

6

This code will group the chars in groups of 3, and convert each group to a string.

string s = "abcdefghij";

var split = s.Select((c, index) => new {c, index})
    .GroupBy(x => x.index/3)
    .Select(group => group.Select(elem => elem.c))
    .Select(chars => new string(chars.ToArray()));

foreach (var str in split)
    Console.WriteLine(str);

prints

abc
def
ghi
j

Fiddle: http://dotnetfiddle.net/1PgFu7

dcastro
  • 66,540
  • 21
  • 145
  • 155
3

Using a bit of Linq

static IEnumerable<string> Split(string str)
{
    while (str.Length > 0)
    {
        yield return new string(str.Take(3).ToArray());
        str = new string(str.Skip(3).ToArray());
    }
}

Here is the Demo

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
2
IEnumerable<string> GetNextChars ( string str, int iterateCount )
{
    var words = new List<string>();

    for ( int i = 0; i < str.Length; i += iterateCount )
        if ( str.Length - i >= iterateCount ) words.Add(str.Substring(i, iterateCount));
        else words.Add(str.Substring(i, str.Length - i));

    return words;
}

This will avoid ArgumentOutOfRangeException in @Sajeetharan's answer.

Edit: Sorry for completely dumb previous answer of mine :) this is supposed to do the trick.

Tolga Evcimen
  • 7,112
  • 11
  • 58
  • 91
  • Doesn't even compile... mismatch on the return type. For some reason @Sajeetharan edited out the key bit that would make this work, which just made their answer doubly bad – musefan Apr 17 '14 at 10:32
  • All you needed to do was add `yield` to your previous answer ;) – musefan Apr 17 '14 at 10:40
  • 1
    Sadly I don't know the usage of the `yield` keyword. I will certainly look into that. Thanks. – Tolga Evcimen Apr 17 '14 at 10:41
1
IEnumerable<string> Split(string str) {
    for (int i = 0; i < str.Length; i += 3)
        yield return str.Substring(i, Math.Min(str.Length - i, 3));
}
Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
1

No, I don't believe it is possible using just string.Split(). But it is simple enough to create your own function...

string[] MySplit(string input)
{
   List<string> results = new List<string>();
   int count = 0;
   string temp = "";

   foreach(char c in input)
   {
      temp += c;
      count++;
      if(count == 3)
      {
         result.Add(temp);
         temp = "";
         count = 0;
      }
   }

   if(temp != "")
      result.Add(temp);

   return result.ToArray();
}
musefan
  • 47,875
  • 21
  • 135
  • 185
  • I'd use a string builder or a `char[]` rather than repeated concatenations like this. – Rawling Apr 17 '14 at 10:23
  • @Rawling: Maybe... not really worth worrying about it in my opinion, but obviously no harm going the extra step if it suits your preference – musefan Apr 17 '14 at 10:26