Search code examples
stringlinqchunks

linq , string to chunks


I need to split string to chunks by linq

example I want to cut this word(Like) too chunks and the length of each chunk is 2 letters

the result will be Li ik ke

there is sequences here.

Please, advise me.


Solution

  • string s = "Like";    
    string s2 = String.Join(" ", s.Select((x, index) => (index+1) == s.Length ? "" : String.Concat(x, s[index+1])));
    

    UPDATE

    By the way, the same problem could be solved with Regex:

    string s3 = Regex.Replace(s, @"\w", m => m.NextMatch().Success ? m.Value + m.NextMatch().Value + " " : "").TrimEnd();