我正在检查这些字符串,看它们是否包含单词"hi",如果它们返回true则返回true.否则我会回复假.字符串"high up应返回false但返回true.我该如何解决这个问题?
public static bool StartHi(string str)
{
if (Regex.IsMatch(str, "hi"))
{
return true;
}
else
return false;
}
static void Main(string[] args)
{
StartHi("hi there"); // -> true
StartHi("hi"); // -> true
StartHi("high up"); // -> false (returns true when i run)
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*exD 14
尝试指定单词边界(\b):
if(Regex.IsMatch(str, @"\bhi\b"))
Run Code Online (Sandbox Code Playgroud)