如何统计字符串内子字符串的数量
我正在尝试读取文本文件并计算某个字符串出现的次数。这是我到目前为止有:如何统计字符串内子字符串的数量
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.
如果你想显示每行的计数意味着你必须将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);
啊谢谢你!修复它 –
很高兴听到它的帮助。总是乐意帮助你.......! –
如何将所有包含单词“the”的字符串保存到数组中? –
您需要增量次数,而不是每次设置它
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();
我认为这种正则表达式不会足够。如果一个单词包含_the_两次,它将被重复两次。也许[\ b。*?。*?\ b](http://regexstorm.net/tester?p=%5cb.*%3fthe.*%3f%5cb&i=hello%0d%0ahethello%0d%0ahethelthelo)是一个可以工作的正则表达式。 –
的。如果你使用.NET 3.5你可以在一个班轮与LINQ做到这一点:
int count = line.Count(f => f == 'the');
var allLines = File.ReadAllLines(@"C:\POC\input.txt");
var theCount = allLines.SelectMany(l => l.Split(' '))
.Where(l => l.ToLower().Contains("the"))
.Count();
@Verarind:https://msdn.microsoft.com/en-us/library/s2tte0y1(v=vs.110).aspx 'ReadAllLines'方法负责处理它。 – DarkKnight
噢 - 是的。错读代码。我读了'ReadAllText'。对不起 - 我的错。 –
逐行读取时和行添加数行,您可以使用内循环下面的代码。
Regex.Matches(Regex.Escape(input), "the", RegexOptions.IgnoreCase).Count
每个循环都要设置计数。你想增加计数。 'counter + = ....' – Nkosi