繁体   English   中英

如何显示使用C#在字符串中找到的单词?

[英]How to display words found in a string using C#?

虽然我可以使用regex替换单词,但这是我的代码:

public static string replacestring(string input)
{
   var Words = "memory|buffer overflow|address space|stack overflow|call stack";
   string cleanword = Regex.Replace(input, @"" + Words + "", "", RegexOptions.IgnoreCase);
   return cleanword;
}

这是字符串值:

在软件中,如果调用堆栈指针超出堆栈界限,则会发生堆栈溢出。 调用堆栈可能包含有限的地址空间,通常在程序开始时就确定了地址空间。 调用堆栈的大小取决于许多因素,包括编程语言,计算机体系结构,多线程和可用内存量。 当程序尝试使用超出调用堆栈可用空间的空间时(也就是说,当它尝试访问超出调用堆栈范围的内存时,这实际上是缓冲区溢出),该堆栈被称为溢出,通常会导致程序崩溃。

如何拾取并显示在字符串中找到的单词并计数?

例如

找到:堆栈溢出,地址空间,调用堆栈,内存,缓冲区溢出
次数:5

预先感谢一百万,由此带来的不便,我们深表感谢。

您可以从以下内容开始:

string wordsToMatch = "memory|buffer overflow|address space|stack overflow|call stack";
string input = "In software, a stack overflow occurs if the call stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory. When a program attempts to use more space than is available on the call stack (that is, when it attempts to access memory beyond the call stack's bounds, which is essentially a buffer overflow), the stack is said to overflow, typically resulting in a program crash.";

var wordsFound = new List<string>();
foreach (string word in wordsToMatch.Split('|')) {
    foreach (Match match in Regex.Matches(input, word, RegexOptions.IgnoreCase)) {
       if (match.Value.Equals(word)) {
           wordsFound.Add(match.Value);
       }
    }
}
Console.WriteLine("Found: " + string.Join(",", wordsFound.Distinct()));
Console.WriteLine("Count: " + wordsFound.Distinct().Count());

输出:

找到:内存,缓冲区溢出,地址空间,堆栈溢出,调用堆栈

数:5

http://rextester.com/CYRL45262

您可以使用LINQ删除重复项并返回数组的计数。

var count = Regex.Matches(str, pattern).OfType<Match>().Select(m => m.Groups[0].Value).Distinct().ToArray().Count();

输出:

5

代码演示

您可以像这样使用字符串的拆分功能:

var result = Words.split(new char[] {'|', ' '}) ;
var wordCount = result.Length;

这是使用linq作为替代方案的解决方案:这也解决了OP中隐含的唯一性。

var toMatch = "memory|buffer overflow|address space|stack overflow|call stack";
Regex reg = new Regex(toMatch, RegexOptions.IgnoreCase);
var result = reg.Matches(input)
                .OfType<Match>()
                .Where  (m => m.Success)
                .GroupBy(m => m.Value)
                .Select (m => new { Word = m.First().Value, QtyFound = m.Count() });

var wordsFound = string.Join(",", result.Select(r => r.Word));
var totalFound = result.Sum(r => r.QtyFound);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM