C#程序检查给定字符串中是否存在子字符串
使用contains()C#中的方法检查子字符串是否在给定的字符串中。
让我们说字符串是-
United
在字符串中,您需要找到子字符串“Uni”。为此,请使用contains方法,并像下面的代码片段一样使用它-
res = str1.Contains(str2);
示例
您可以尝试运行以下代码以在字符串中找到子字符串。
using System;
public class Demo {
public static void Main() {
string str1 = "United", str2 = "Uni";
bool res;
res = str1.Contains(str2);
if (res)
Console.Write("The substring " + str2 + " is in the string " + str1);
else
Console.Write("The substring " + str2 + " is not in the string " + str1);
}
}输出结果
The substring Uni is in the string United
热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短