To get the wordcount of a string in C# and VB.NET you can use the following snippet.

Sample C#

public static int WordCount(string s)
{
	return Regex.Matches(s, @"[\S]+").Count;
}

Sample VB.NET

Public Shared Function WordCount(s As String) As Integer
	Return Regex.Matches(s, "[\S]+").Count
End Function

One thought on “How to get the wordcount of a string in C# and VB.NET”

Leave a Reply