c#if String包含2"hallo"

eMi*_*eMi 4 c# string

可能重复:
您如何计算字符串(C#)中字符串的出现次数?

我想检查String是否包含2件事..

String hello = "hellohelloaklsdhas";

if hello.Contains(*hello 2 Times*); -> True
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

Ray*_*Ray 10

你可以使用正则表达式:)

return Regex.Matches(hello, "hello").Count == 2;
Run Code Online (Sandbox Code Playgroud)

这匹配hello模式的字符串,"hello"如果计数为2则返回true.


m0s*_*it0 5

常用表达.

if (Regex.IsMatch(hello,@"(.*hello.*){2,}"))
Run Code Online (Sandbox Code Playgroud)

我猜你的意思是"你好",这将匹配一个至少有2个"你好"的字符串(不完全是2个"你好")