Search code examples
c#arrayssplit

How to split a BodyText (Sentences) into specific number of words and put them in an array


I want to split a BodyText (some Sentences = over 3000 words) into a specific number of words (for example 500 words), and put them in an array. Means that every block of that array should have 500 words and we will have the rest in the last block.


Solution

  • If the general character to split words on is a space, try this. It splits the words on a whitespace, then it puts them in a list by groups of 500:

    string[] sentences = bodyText.Split(' ');
    
    List<string[]> parts = new List<string[]>();
    for (int i = 0; i < sentences.Length; i += 500)
    {
        parts.Add(sentences.Skip(i).Take(500).ToArray());
    }
    

    If you want the end result in an array, you could use ToArray():

    string[][] endArray = parts.ToArray();