有時(shí)我們可能要判斷一個(gè)字符串中是否要包含指定的字符串,或者是判斷一個(gè)字符串從指定位置開(kāi)始是否包含指定的子字符串,這分別可以借助IndexOf(string value)方法和IndexOf(string value, int startIndex)方法來(lái)實(shí)現(xiàn)。
IndexOf(string value)方法在前面的文章中已探討(http://www.companysz.com/article/Csharp/jichu/2012/5669.html),本文重點(diǎn)探討IndexOf(string value,int startIndex)的使用。
IndexOf(string value, int startIndex)是從指定位置startIndex開(kāi)始搜索字符串value的位置。該重載函數(shù)有兩個(gè)參數(shù),第一個(gè)參數(shù)value是要搜索的子字符串,startIndex是要搜索的起始位置。
IndexOf(string value,int startIndex)方法的返回值為int型,如果字符串中包含value值,則返回第一次找到value的位置值(該值從0開(kāi)始計(jì)算),否則返回-1。
該方法在搜索子字符串時(shí)時(shí)區(qū)分大小寫(xiě)和區(qū)域性的。
IndexOf(string value, int startIndex)方法的一個(gè)例子如下:
string str = "武林網(wǎng)VEVB,你我共同的樂(lè)園。";
int iPos = str.IndexOf("樂(lè)園", 7);
上面這個(gè)例子的返回值為: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 )
|
新聞熱點(diǎn)
疑難解答
圖片精選