C# index of 用法(转载)
来源:互联网 发布:太阳系模拟软件下载 编辑:程序博客网 时间:2024/06/08 13:42
查找字串中指定字符或字串首次出现的位置,返首索引值,如:
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。 如果没有找到则返回-1。
如果需要更强大的字符串解析功能应该用Regex类,使用正则表达式对字符串进行匹配。
indexof() :在字符串中从前向后定位字符和字符串;所有的返回值都是指在字符串的绝对位置,如为空则为- 1
string test="asdfjsdfjgkfasdsfsgfhgjgfjgdddd";test.indexof(’d’) =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 同上。
原文地址
0 0
- C# index of 用法(转载)
- Index of c#
- index of 的用法与subString的用法
- Index of /
- 引用 网页层叠z-index用法详解(转载)
- 【转载】C#中splitContainer用法
- C#中File 类的用法 转载
- C#中dataGridView用法集(转载)
- C#中messagebox用法(转载)
- 【转载】c# sealed class 的用法
- Index of American Universities
- Index of /MPlayer/releases
- Index of Unix
- Index of open source
- Index of Java
- Index of Test
- Index of C++
- Index of OOAD
- string.Format
- C# 将XmlDocument转化为string函数
- C# POST XML(修正中文乱码问题)
- C# 将字符串(符合xml格式)转换为XmlDocument
- C# 将日志写入文本文件
- C# index of 用法(转载)
- 【3Dsmax】 反向运动学的原理及设置
- Substring的用法
- C#中各种编码格式的区别
- XML 声明必须是文档中的第一个节点并且在它之前不允许出现空白字符
- IAR错误之Error[Li005] no definition for "__program_start"
- select 1 from 表名
- select * from table where 1<>1
- select * from 表名 where 1=1