使用c#在字符串中查找子字符串的最佳方法是什么?

Ris*_*876 2 c# substring frequency

我试图在字符串中找到一个单词,意思是

if abcdef ghijk是一个字符串,那么substring ghijk应该给我们1作为频率.但是如果substring是cde那么应该返回0.

我希望这可以用正则表达式,但我不知道是不是.或者可能是字符串类中有一个方法.

Rom*_*kar 5

如果你想要单词频率,你可以创建单词频率.像这样的字典:

s.Split().GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count())
Run Code Online (Sandbox Code Playgroud)

然后检查这本词典中是否有单词.

var s = "abcdef ghijk abcdef";
var d = s.Split().GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count())
// Dictionary<string, int>(2) { { "abcdef", 2 }, { "ghijk", 1 } }
Run Code Online (Sandbox Code Playgroud)