Count words and spaces in string C#

int countSpaces = mystring.Count(Char.IsWhiteSpace); // 6
int countWords = mystring.Split().Length; // 7

Note that both use Char.IsWhiteSpace which assumes other characters than " " as white-space(like newline). Have a look at the remarks section to see which exactly .


you can use string.Split with a space http://msdn.microsoft.com/en-us/library/system.string.split.aspx

When you get a string array the number of elements is the number of words, and the number of spaces is the number of words -1