C# - How to count Words and letters from a .RTF / .TXT File?
The Title says everything..
Searched Google and Stackoverf开发者_开发问答low and didn't find something similiar..
For .txt
file you can use regexp \b\w+\b
. It will match all occurrences of words, e.g.:
var count = Regex.Matches(input, @"\b\w+\b").Count;
To count letters:
int count = input.Count(char.IsLetter);
static void Main()
{
const string t1 = "To be or not to be, that is the question.";
Console.WriteLine(WordCounting.CountWords1(t1));
Console.WriteLine(WordCounting.CountWords2(t1));
const string t2 = "Mary had a little lamb.";
Console.WriteLine(WordCounting.CountWords1(t2));
Console.WriteLine(WordCounting.CountWords2(t2));
}
more is here
getting text from rtf - Get plain text from an RTF text
count words in text - http://www.dotnetperls.com/word-count
精彩评论