Ajit Hegde
Ajit Hegde

Reputation: 550

is there any library function for word counting in c#..?

I am wondering is there any functions in c# that helps to count number of words,sentences in a file...?

Now I used the following code is it efficient/error free?

string[] words=datafromfile.Split(' ');
int numberofwords=words.GetUpperBound(0)

Thank you

Upvotes: 2

Views: 444

Answers (3)

Roy Dictus
Roy Dictus

Reputation: 33139

Not out of the box, but you can look at this.

Upvotes: 0

Oded
Oded

Reputation: 499002

You could use RegEx.Split using the word boundary \b instead.

Upvotes: 5

Muhammad Hasan Khan
Muhammad Hasan Khan

Reputation: 35126

You're not considering new lines as word boundaries. Full stop is also a word boundary. You should use regex for this. It has \b

Use int words = Regex.Split(yourText, "\b").Length

Upvotes: 1

Related Questions