C# 判断字符串一是否包含字符串二

方法一:

            Console.WriteLine("请输入第一个字符串:");
            string str1 = Console.ReadLine();
            Console.WriteLine("请输入第二个字符串:");
            string str2 = Console.ReadLine();

            int count1 = str1.Length;          //取得第一个字符串的长度
            int count2 = str2.Length;          //取得第二个字符串的长度
            for (int i = 0; i < count1; i++)
            {
                int index = i;                         //将i值赋给变量index
                for (int j = 0; j < count2; j++)
                {
                    if (str1[index] != str2[j])          //取得两个字符串的下标进行比较
                    {
                        break;                                   //判断如果字符串一下标对应的值不等于字符串二下示对应的值时就跳出循环
                    }
                    else
                    {
                        index++;
                    }
                    if (j == count2 - 1)
                    {
                        Console.WriteLine(str1+"中包含"+str2);
                        return;
                    }
                }
            }

 

方法二:

            Console.WriteLine("请输入第一个字符串:");
            string str1 = Console.ReadLine();
            Console.WriteLine("请输入第二个字符串:");
            string str2 = Console.ReadLine();

             if (str1.Contains(str2))
            {
                Console.WriteLine(str1+"中包含"+str2);
            }
            else
            {
                Console.WriteLine(str1);
            }

posted on 2008-09-03 00:39  VictorShan  阅读(8283)  评论(0)    收藏  举报

导航