In c# how do I count the number of words in each index of a string array?

For example I've got a text file that I've put into an array and then I have split that array by the full stops (so each sentence is in its own index of the new array) using the following:

textSplitArray = textArray[j].Split('.');

How would I then count the number of words in each index of textSplitArray to determine which sentence has the most words? Is it possible to do this or would I have to do it another way?

I've tried searching everywhere but can't seem to find an answer


If you want to know which sentence is the longest (i.e. contains maximum words) use

var result = textSplitArray.OrderByDescending(x => x.Split(' ').Length)

And if you want to know number of words in that longest sentence, use

int Max = textSplitArray.Max(x => x.Split(' ').Length);


int Max = result.Length;

Since every two words in a sentence can be separated by space, that's why i have split each sentence based on ' ' space.

string[] textSplitArray = textArray[j].Split('.');
        foreach (string s in textSplitArray)
            int NumberOfWords = s.Split(' ').Length;                

you can use lambada expression like this :

 var max = textSplitArray.Select((s,i) =>
        return new { value = s.Split(' ').Count() , index = i};
 int maxValue = max.value;
 int maxPos = max.index;

 //this will return anonymous object that has two properties : {index  , value} that refers to item's value and it's position in the sequence.

check if that help.

 string[] source = textSplitArray.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);

source.Length() is your count.

Need Your Help

JQuery retrieving text instead of value of select list

javascript jquery

I need to get the value from a select list but JQuery is returning the text within the options of the select.

How to pass list of sets as separate arguments into function?

python list function set

I've created a list of sets that I'd like to pass into set.intersection()