Get only Whole Words from a .Contains() statement

后端 未结 4 517
再見小時候
再見小時候 2020-12-20 15:09

I\'ve used .Contains() to find if a sentence contains a specific word however I found something weird:

I wanted to find if the word "hi" was present in a se

4条回答
  •  攒了一身酷
    2020-12-20 16:12

    Try using Regex:

    if (Regex.Match(sentence, @"\bhi\b", RegexOptions.IgnoreCase).Success)
    {
        //
    };
    

    This works just fine for me on your input text.

提交回复
热议问题