C#判断字符串中是否包含一个子字符串方法(Contains()和IndexOf())

发布日期:2019-11-24 作者:mi52.com 人气:

C#判断字符串中是否包含一个子字符串方法(Contains()和IndexOf())


1. 判断一个字符串中是否包含另一个子字符串时,可使用 IndexOf();

string str = "ABC@QQ";

if(str.IndexOf("@")>=0){

    //any other code

}


2. 其实C#中还定义了Contains()这样的方法,使用如下:


String abc = "dfadfadf@dfdasdfioa@";

bool result = abc.Contains("@");

            

与Contains()类似的方法,在集合中也有定义到,如:

List, Set 以及数组 Array 的 Contains(),

Dictionary的 ContainsKey()和ContainsValue()


返回顶部

拨打电话
首页