Get only Whole Words from a .Contains() statement

后端 未结 4 521
再見小時候
再見小時候 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 15:53

    You could write your own extension method for string like:

    static class StringExtension
            {
                public static bool ContainsWord(this string s, string word)
                {
                    string[] ar = s.Split(' ');
    
                    foreach (string str in ar)
                    {
                        if (str.ToLower() == word.ToLower())
                            return true;
                    }
                    return false;
                }
            }
    

提交回复
热议问题