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 好听的元旦简短祝福语