Friday, 27 September 2013

Split string in group of n number (Ex : Group of 3)

Today , I needed one solution for spliting string in group of 3

String as "Ac1Imghsd345dsda"

My program is

static void Main(string[] args)
{IEnumerable<string> result = Split("Ac1Imghsd345dsda", 3);
}static IEnumerable<string> Split(string str, int groupSize)
{
return Enumerable.Range(0, str.Length / groupSize)
.Select(i => str.Substring(i * groupSize, groupSize));
}


I get an output strings in group of 3 as Enumerable form

Here no to need to convert in toCharArray then FOR Loop..

Simply do as shows....

 

No comments:

Post a Comment