2016-11-24 105 views
-1

我正在嘗試讀取文本文件並計算某個字符串出現的次數。這是我到目前爲止有:如何統計字符串內子字符串的數量

System.IO.StreamReader file = new System.IO.StreamReader("C:\\Users\\Test\\Documents\\Sample.txt"); 
while ((line = file.ReadLine()) != null) { 
    Console.WriteLine(line); 

    counter = Regex.Matches(line, "the", RegexOptions.IgnoreCase).Count; 
} 

Console.WriteLine(counter); 

file.Close(); 

// Suspend the screen. 
Console.ReadLine(); 

所以我想找到所有包含在其中「的」字符串的話,但我沒有得到正確的計數。我希望它也能像「枯萎」等詞語一樣統計「the」,而不僅僅是「the」這個詞。我發現的問題是,當txt文件在它們之間包含不同的段落和空格時,它會錯過這些單詞。當我在段落之間沒有空格時,它似乎工作。我可以做些什麼來解決這個問題?

這就是我所說的一段空間:

Sample text Sample text Sample text Sample text Sample text. 

Sample text Sample text Sample text Sample text Sample text . 

但是,如果我將它們合併這樣它的工作原理:

Sample text Sample text Sample text Sample text Sample text.Sample text Sample text Sample text Sample text Sample text. 
+2

每個循環都要設置計數。你想增加計數。 'counter + = ....' – Nkosi

回答

1

如果你想顯示每行的計數意味着你必須將Console.WriteLine(counter);移到while的邊界。

string searchStr= "the"; 
while ((line = file.ReadLine()) != null) 
{ 
    Console.WriteLine(line); 
    counter = Regex.Matches(line,searchStr, RegexOptions.IgnoreCase).Count; 
    Console.WriteLine("Count of {0} in this line is {1}",searchStr,counter); 
} 

否則如果更新的同時,每個迭代counter可以顯示搜索詞的完整計數。

string searchStr= "the"; 
while ((line = file.ReadLine()) != null) 
{ 
    Console.WriteLine(line); 
    counter += Regex.Matches(line, searchStr , RegexOptions.IgnoreCase).Count; 
} 
Console.WriteLine("Occurance of {0} in this document is {1}",searchStr,counter); 

更新:要獲得包含特定單詞的所有單詞和計數的搜索字符串出現的總數在給定的內容,你可以使用一個List 的類似如下:

string searchStr= "the"; 
List<string> totalMatchStrings = new List<string>(); 
while ((line = file.ReadLine()) != null) 
{ 
    totalMatchStrings.AddRange(lineInput.Split(' ').Where(x => x.ToLower().Contains(searchString)));   
} 
string matchingWords = String.Join(",", totalMatchStrings.Distinct()); 
Console.WriteLine("Occurance of {0} in this document is {1}",searchStr,totalMatchStrings.Count); 
Console.WriteLine("matching words are : {0}",matchingWords); 
+0

啊謝謝你!修復它 –

+0

很高興聽到它的幫助。總是樂意幫助你.......! –

+0

如何將所有包含單詞「the」的字符串保存到數組中? –

2

您需要增量次數,而不是每次設置它

System.IO.StreamReader file = new System.IO.StreamReader("C:\\Users\\Test\\Documents\\Sample.txt"); 
while ((line = file.ReadLine()) != null) 
{ 
    Console.WriteLine(line); 
    //increment count instead of setting it everytime 
    counter += Regex.Matches(line, "the", RegexOptions.IgnoreCase).Count; 
} 
Console.WriteLine(counter); 
file.Close(); 
// Suspend the screen. 
Console.ReadLine(); 
+0

我認爲這種正則表達式不會足夠。如果一個單詞包含_the_兩次,它將被重複兩次。也許[\ b。*?。*?\ b](http://regexstorm.net/tester?p=%5cb.*%3fthe.*%3f%5cb&i=hello%0d%0ahethello%0d%0ahethelthelo)是一個可以工作的正則表達式。 –

0

的。如果你使用.NET 3.5你可以在一個班輪與LINQ做到這一點:

int count = line.Count(f => f == 'the'); 
1
var allLines = File.ReadAllLines(@"C:\POC\input.txt"); 
var theCount = allLines.SelectMany(l => l.Split(' ')) 
     .Where(l => l.ToLower().Contains("the")) 
     .Count(); 
+0

@Verarind:https://msdn.microsoft.com/en-us/library/s2tte0y1(v=vs.110).aspx 'ReadAllLines'方法負責處理它。 – DarkKnight

+0

噢 - 是的。錯讀代碼。我讀了'ReadAllText'。對不起 - 我的錯。 –

0

逐行讀取時和行添加數行,您可以使用內循環下面的代碼。

Regex.Matches(Regex.Escape(input), "the", RegexOptions.IgnoreCase).Count