怎样用C#判断一个字符串是否包含另一个字符串?简单吗

2025-03-14 12:26:16
推荐回答(1个)
回答(1):

//容易,用String类的Contains方法就可以判断,具体看例子。
using System;

public class Test
{
public static void Main(string[] args)
{
string line="aaa 你好 哈哈哈 对了 good bye just so so so easy!";
string[] strs={"aaa","sos","哈哈哈","对了吗","so easy"};

foreach(string s in strs)
{
if(line.Contains(s))
{
Console.WriteLine("字符串\""+line+"\"包含字符串\""+s+"\"");
}
else
{
Console.WriteLine("字符串\""+line+"\"不包含字符串\""+s+"\"");
}
}
Console.Write("Press any key to continue . . . ");
Console.ReadLine();
}

}