C#中字符串操作函數IndexOf()是用來查找字串中指定字符或字串首次出現的位置,返回其首次出現的索引值,方法原型如下:
public int IndexOf (string value)
參數value為要尋找的字符串值,返回值為整數類型,如果被查找的字符串中包含要查找的字符串value則返回其所在位置的索引(從0開始的索引值),否則返回-1。
如:
string str = "武林網VEVB歡迎您。";
int iPos = str.IndexOf("IT");
則iPos的值為:3
再如:
string str = "武林網VEVB歡迎您。";
int iPos = str.IndexOf("www");
其iPos值為:-1
同時,IndexOf方法還有其它8種重載形式:
(1)public int IndexOf(char value)
(2)public int IndexOf(char value, int startIndex)
(3)public int IndexOf( string value, int startIndex)
(4)public int IndexOf(string value,StringComparison comparisonType )
(5)public int IndexOf(char value, int startIndex,int count )
(6)public int IndexOf(string value,int startIndex,int count )
(7)public int IndexOf(string value,int startIndex,StringComparison comparisonType )
(8)public int IndexOf(string value,int startIndex,int count, StringComparison comparisonType )
新聞熱點
疑難解答