Search code examples
c#asp.netstringlinqchunks

Splitting C# string into 3 word chunks using regex or other method


I would like to make a feature where a string in C# can be broken into 3 word chunks like this:

Today is a nice day, and I have been driving a car /*(don't laugh lol - not a part of sentence)*/

So the first thing that I would like to do is throw out all special characters from string except for numbers and letters.

And once I do that, then goes the breaking the word into a 3 word chunk, where the output in the case of upper sentence would be:

Today is a 
nice day and
I have been
driving a car

I was thinking to do this via regex, but then again there's LINQ methods and all that could solve this easily, so I'm not really sure which way to choose to choose in order to make this? What would be the most efficient way?

P.S. Also a problem that I'm thinking of is what if a word has 8 words and I want to make it into 3 chunk words...? How would I then throw out the last 2 words that don't match the criteria of forming the 3 chunk "sentence"?

Can someone help me out ?


Solution

  • string str = "Today is a nice day, and I have been driving a car";
    
    str =  Regex.Replace(str, "[^0-9a-zA-Z ]+", "");
    
    string[] arr = str.Split(' ');
    int nElements = 0;
    
    for (int i = 0; i < arr.Length; i+=3)
    {
        if(i+3 < arr.Length)
        {
            nElements = 3;
        }
        else
        {
            nElements = arr.Length - i;
        }
    
        Console.WriteLine(arr.Skip(i).Take(nElements).Aggregate((current, next) => current + " " + next));
    }