欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

IndexOf()和Substring()方法,indexofsubstring

来源: javaer 分享于  点击 7088 次 点评:223

IndexOf()和Substring()方法,indexofsubstring


  String.IndexOf 方法 (value, [startIndex], [count])   报告指定字符在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。   参数   value   要查找的 Unicode 字符。 对 value 的搜索区分大小写。   startIndex(Int32)   可选项,搜索起始位置。不设置则从0开始。   count(Int32)   可选项,要检查的字符位数。   返回值   如果找到该字符,则为 value 的索引位置;否则如果未找到,则为 -1。   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

  public String substring(int beginIndex)返回一个新的字符串,它是此字符串的一个子字符串。该子字符串始于指定索引处的字符,一直到此字符串末尾。

  例如:

  "unhappy".substring(2) returns "happy"

  "Harbison".substring(3) returns "bison"

  "emptiness".substring(9) returns "" (an empty string)

  参数:

  beginIndex - 开始处的索引(包括)。

  返回:

  指定的子字符串。

  抛出:

  IndexOutOfBoundsException - 如果 beginIndex 为负或大于此 String 对象的长度。

  --------------------------------------------------------------------------------

  substring

  public String substring(int beginIndex,

  int endIndex)返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始, endIndex:到指定的 endIndex-1处结束。

  示例:

  "hamburger".substring(4,8) returns "urge"

  "smiles".substring(1,5) returns "mile"

  参数:

  beginIndex - 开始处的索引(包括)。

  endindex 结尾处索引(不包括)。

  返回:

  指定的子字符串。

  抛出:

  IndexOutOfBoundsException - 如果 beginIndex 为负,或length大于字符串长度。

  -----------------------------------------------------以下为rongew更新--------------------------------------------

  C#中 substring() 有两个重载函数

  substring(int a) 用法同上

  substring(int a,int b) b参数应该为截取后的字符串长度

  上面string得用法不只是针对哪种语法

  针对的是c#

相关文章

    暂无相关文章

用户点评