当前位置:K88软件开发文章中心编程语言.NET.NET01 → 文章内容

C#中字符串操作函数IndexOf(string)的使用

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2018-12-31 11:27:23

:2012-06-16 20:38:53

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)的使用