IndexOf(char value,int startIndex)函數是IndexOf重載函數形式之一,其從指定的startIndex位置開始查找指定的字符value,如果字符串中包含value值,則返回其第一次出現的位置(該位置從索引0開始)。該函數的原型為:
public int IndexOf( char value, int startIndex )
參數:
value就是要查找的字符,startIndex就是搜索的起始位置。
返回值:
該函數的返回值為整型(int)。如果找到該字符,則返回值為value 的從零開始的索引位置;如果未找到,則返回值為 -1。
此方法執行順序搜索,即僅當 Unicode 標量值相同時兩個字符才被視為相等。
舉例如下:
string str = "武林網VEVB,你我共同的樂園。";
int iPos = str.IndexOf(7,'樂');
這個例子中,iPos的值為13,而不是5,原因是指定了搜索的起始位置為7,即從位置7后開始搜索相應的字符。
該函數還有其它8種重載形式:
(1)public int IndexOf(char value)
(2)public int IndexOf(string value)
(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 )
新聞熱點
疑難解答