C#中indexof和substring函数用法

"abcdefg".substring(4,2)
返回的值为:ef
从字符串"abcdefg"中第4位开始取,取到第2位。

"abcdefg".substring(4)
返回:efg
从字符串"abcdefg"中第4位开始取,取到字符串的尾部

Code

 

5月以来,多家<A href="http://www.baidu.com"><FONT color=#ee3d11>基金</FONT></A>公司...

temp=5月以来,多家<A
temp2= href="http://www.baidu.com"><FONT color=#ee3d11>基金</FONT></A>公司...
temp3=5月以来,多家<A href="http://www.baidu.com">
temp4=<FONT color=#ee3d11>基金</FONT></A>公司...
temp5=<FONT color=#ee3d11>基金</FONT>
temp7=公司...

 

temp6=<FONT color=#ee3d11>基<!---->金</FONT>
result=5月以来,多家<A href="http://www.baidu.com"><FONT color=#ee3d11>基<!---->金
</FONT></a>
result+temp7=5月以来,多家<A href="http://www.baidu.com"><FONT color=#ee3d11>基<!---->
金</FONT></a>公司...

 

indexof() :在字符串中从前向后定位字符和字符串;所有的返回值都是指在字符串的绝对位置,如为空则为- 1

string test="asdfjsdfjgkfasdsfsgfhgjgfjgdddd";

test.indexof('d')      =2           //从前向后 定位 d 第一次出现的位置
test.indexof('d',1)    =2          //从前向后 定位 d 从第三个字符串 第一次出现的位置
test.indexof('d',5,2) =6     //从前向后 定位 d 从第5 位开始查,查2位,即 从第5位到第7位;

lastindexof() :在字符串中从后向前定位字符和字符串;、
用法和 indexof() 完全相同。


下面介绍 IndexOfAny ||lastindexofany

他们接受字符数组做为变元,其他方法同上,返回数组中任何一个字符最早出现的下标位置

如下

        char[] bbv={'s','c','b'};
        string abc = "acsdfgdfgchacscdsad";
       
        Response.Write(abc.IndexOfAny(bbv))=1
        Response.Write(abc.IndexOfAny(bbv, 5))=9
        Response.Write(abc.IndexOfAny(bbv, 5, 3))=9

lastindexofany 同上。

 

你可能感兴趣的:(substring)