有時我們可能要判斷一個字符串中是否要包含指定的字符串,或者是判斷一個字符串從指定位置開始是否包含指定的子字符串,這分別可以借助IndexOf(string value)方法和IndexOf(string value, int startIndex)方法來實現。
IndexOf(string value)方法在前面的文章中已探討(http://www.49028c.com/article/Csharp/jichu/2012/5669.html),本文重點探討IndexOf(string value,int startIndex)的使用。
IndexOf(string value, int startIndex)是從指定位置startIndex開始搜索字符串value的位置。該重載函數有兩個參數,第一個參數value是要搜索的子字符串,startIndex是要搜索的起始位置。
IndexOf(string value,int startIndex)方法的返回值為int型,如果字符串中包含value值,則返回第一次找到value的位置值(該值從0開始計算),否則返回-1。
該方法在搜索子字符串時時區分大小寫和區域性的。
IndexOf(string value, int startIndex)方法的一個例子如下:
string str = "武林網VEVB,你我共同的樂園。";
int iPos = str.IndexOf("樂園", 7);
上面這個例子的返回值為:13
IndexOf方法有9種重載形式,其它8種分別如下:
(1)public int IndexOf(char value)
(2)public int IndexOf(string value)
(3)public int IndexOf( char 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 )
新聞熱點
疑難解答