C# string属性IndexOf的用法


IndexOf() 
查找字串中指定字符或字串首次出现的位置,返首索引值,如: 
str1.IndexOf("字"); //查找“字”在str1中的索引值(位置) 
str1.IndexOf("字串");//查找“字串”的第一个字符在str1中的索引值(位置) 
str1.IndexOf("字",start,end);//从str1第start+1个字符起,查找end个字符,查找“字”在字符串STR1中的位置[从第一个字符算起]注意:start+end不能大于str1的长度


indexof参数为string,在字符串中寻找参数字符串第一次出现的位置并返回该位置。如string s="0123dfdfdf";int i=s.indexof("df");这时i==4。 
如果需要更强大的字符串解析功能应该用Regex类,使用正则表达式对字符串进行匹配。






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() 用法

string a="aadsfdjkfgklfdglfd"

a.substring(5)      //截取从第五位以后的所有字符串

a.substring(0,5)     //截取从第0到第5 以后的所有字符串



 

C# 中的 StringSearch 是一个用于字符串匹配的类,它提供了多种字符串查找和替换的方法。下面是一些常用的 StringSearch 方法及其用法: 1. IndexOf(string value):查找指定字符串在当前字符串中第一次出现的位置。 ```csharp string str = "hello world"; int index = str.IndexOf("world"); // index = 6 ``` 2. LastIndexOf(string value):查找指定字符串在当前字符串中最后一次出现的位置。 ```csharp string str = "hello world"; int index = str.LastIndexOf("l"); // index = 9 ``` 3. Contains(string value):判断当前字符串是否包含指定字符串。 ```csharp string str = "hello world"; bool result = str.Contains("world"); // result = true ``` 4. Replace(string oldValue, string newValue):将当前字符串中所有的 oldValue 替换为 newValue。 ```csharp string str = "hello world"; string newStr = str.Replace("world", "universe"); // newStr = "hello universe" ``` 5. Split(char[] separator):将当前字符串按照指定字符数组分割成字符串数组。 ```csharp string str = "hello,world"; string[] words = str.Split(new char[] { ',' }); // words = ["hello", "world"] ``` 6. Substring(int startIndex, int length):截取当前字符串的一部分,从 startIndex 开始,长度为 length。 ```csharp string str = "hello world"; string subStr = str.Substring(6, 5); // subStr = "world" ``` 7. ToLower() / ToUpper():返回当前字符串的小写 / 大写形式。 ```csharp string str = "Hello World"; string lowerStr = str.ToLower(); // lowerStr = "hello world" string upperStr = str.ToUpper(); // upperStr = "HELLO WORLD" ``` 以上是 StringSearch 类的一些常用方法及其用法,希望能对你有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值