C#String.Contains()方法
C#中的String.Contains()方法用于返回一个值,该值指示在该字符串中是否出现了指定的子字符串。
语法
public bool Contains (string val);
上面的val是要搜索的字符串。
示例
using System;
public class Demo {
public static void Main(String[] args) {
string str1 = "Akon";
string str2 = "Ak";
Console.WriteLine("String 1 = "+str1);
Console.WriteLine("HashCode of String 1 = "+str1.GetHashCode());
Console.WriteLine("String 2 = "+str2);
Console.WriteLine("HashCode of String 2 = "+str2.GetHashCode());
Console.WriteLine("String 1 is equal to String 2: {0}", str1.Equals(str2));
Console.WriteLine("Does String 1 has String 2 = "+str1.Contains(str2));
}
}输出结果
String 1 = Akon HashCode of String 1 = 416613838 String 2 = Ak HashCode of String 2 = -839927841 String 1 is equal to String 2: False Does String 1 has String 2 = True
示例
using System;
public class Demo {
public static void Main(String[] args) {
string str1 = "Jacob";
string str2 = "John";
Console.WriteLine("String 1 = "+str1);
Console.WriteLine("HashCode of String 1 = "+str1.GetHashCode());
Console.WriteLine("String 2 = "+str2);
Console.WriteLine("HashCode of String 2 = "+str2.GetHashCode());
bool res = str1.Contains(str2);
if (res)
Console.WriteLine("Found!");
else
Console.WriteLine("未找到!");
}
}输出结果
String 1 = Jacob HashCode of String 1 = -790718923 String 2 = John HashCode of String 2 = -1505962600 未找到!
热门推荐
9 足球队祝福语简短
10 他舅生日祝福语简短
11 怀念青春祝福语简短
12 新疆新年祝福语大全简短
13 双节祝福语简短唯美
14 高考送考祝福语简短
15 近代生日祝福语简短精辟
16 明信片祝福语简短句子
17 关于开车的祝福语简短
18 给表弟的祝福语简短