C# 裡可以使用字串的 IndexOfAny() 方法,來一次尋找多個字,並回傳第一次找到的位置。
使用方法
例如今天想要從字串裡是否包含 ^ 、& 、% 等字元,或想找到第一次出現的位置,可以使用以下的語法:
public int IndexOfAny (char[] anyOf);
public int IndexOfAny (char[] anyOf, int startIndex); // 可以從指定的字串位置開始查詢
以此例子來說 position = 5:
string str = "Hello^_^World";
int position = str.IndexOfAny(new char[] { '^', '&', '%' });
找不到時會回傳 -1 給 postition :
string str = "Hello World";
int position = str.IndexOfAny(new char[] { '^', '&', '%' });
此外,還有另一個 LastIndexOfAny() 方法,可以取得最後一次出現的位置,以剛剛的例子再試一次:
string str = "Hello^_^World";
int position = str.LastIndexOfAny(new char[] { '^', '&', '%' });
position = 7。
