C# 搜索关键词

例如有一个字符串“Google Nexus 4 mobile phone”,你要搜索“Google”和“Nexus”,那么应该返回TRUE。

        static void Main(string[] args)
        {
            string[] strArr = { "Nexus","Google"};
            string str = "Google Nexus 4 mobile phone";
            Console.Write(MatchKeyWords(str, strArr));
        }

        static bool MatchKeyWords(string str, string[] keyWords)
        {
            for (int i = 0; i < keyWords.Length; i++)
            {
                if (MatchOneWords(str, keyWords[i]) == false)
                    return false;
            }

            return true;
        }

        static bool MatchOneWords(string str, string word)
        {
            int i = 0;
            int j = 0;
            while (i<str.Length&&j<=word.Length)
            {
                if (str[i]== word[j])
                {
                    i++;
                    j++;
                }
                else
                {
                    i++;
                    j = 0;
                }
                if (j == word.Length)
                {
                    return true;
                }
            }

            return false;
        }
View Code
原文地址:https://www.cnblogs.com/Ligeance/p/3147552.html