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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With