2011-08-19 62 views

回答

3

.txt文件,你可以使用正則表達式\b\w+\b。它將匹配單詞的所有事件,如:

var count = Regex.Matches(input, @"\b\w+\b").Count; 

要計算字母:

int count = input.Count(char.IsLetter); 
+0

謝謝,這工作得很好 - 來算的話。現在只需要字母:d – eMi

+0

好現在,我可以算字母.. – eMi

+0

我的解決方案:私人詮釋GetLetterCount(){ INT 計數= 0 ; StreamReader tr = new StreamReader(「hello.txt」); string text = tr.ReadToEnd(); foreach(char c in text) if(!char.IsWhiteSpace(c)) { count ++; } } return count; } – eMi

1
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)); 
} 

更是here

相關問題