- ·上一篇文章:C#中字符串操作函数String.IndexOf (Char)方法
- ·下一篇文章:C#中的if语句
C#中字符串操作函数IndexOf(string)的使用
C#中字符串操作函数IndexOf()是用来查找字串中指定字符或字串首次出现的位置,返回其首次出现的索引值,方法原型如下:
public int IndexOf (string value)
参数value为要寻找的字符串值,返回值为整数类型,如果被查找的字符串中包含要查找的字符串value则返回其所在位置的索引(从0开始的索引值),否则返回-1。
如:
string str = "翔宇亭IT乐园欢迎您。";
int iPos = str.IndexOf("IT");
则iPos的值为:3
再如:
string str = "翔宇亭IT乐园欢迎您。";
int iPos = str.IndexOf("www");
其iPos值为:-1
同时,IndexOf方法还有其它8种重载形式:
(1)publicint IndexOf(char value)
(2)publicint IndexOf(char value, int startIndex)
(3)publicint IndexOf( string value, int startIndex)
(4)publicint IndexOf(string value,StringComparison comparisonType )
(5)publicint IndexOf(char value, int startIndex,int count )
(6)publicint IndexOf(string value,int startIndex,int count )
(7)publicint IndexOf(string value,int startIndex,StringComparison comparisonType )
(8)publicint IndexOf(string value,int startIndex,int count, StringComparison comparisonType )
C#中字符串操作函数IndexOf(string)的使用