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