gpt4 book ai didi

c# - 检查字符串是否包含特定单词 C#

转载 作者:行者123 更新时间:2023-11-30 13:12:55 24 4
gpt4 key购买 nike

我正在检查这些字符串以查看它们是否包含单词“hi”,如果包含则返回 true。否则我返回 false。字符串“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)
}

最佳答案

尝试指定 word boundaries (\b):

if(Regex.IsMatch(str, @"\bhi\b"))

关于c# - 检查字符串是否包含特定单词 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25110712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com