C#判断字符串中是否包含一个子字符串是可以直接使用Contains()方法

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()

  

原文地址:https://www.cnblogs.com/tommy-huang/p/4803130.html