string 类的常用方法 substring indexof length

string 集中常用的方法:
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 - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。
  示例:
  "hamburger".substring(4, 8) returns "urge"
  "smiles".substring(1, 5) returns "mile"
  参数:
  beginIndex - 开始处的索引(包括)。
  endIndex - 结束处的索引(不包括)。
  返回:
  指定的子字符串。
  抛出:
  IndexOutOfBoundsException - 如果 beginIndex 为负,或 endIndex 大于此 String 对象的长度,或 beginIndex 大于 endIndex。
  -----------------------------------------------------以下为rongew更新--------------------------------------------
  C#中 substring() 有两个重载函数
  substring(int a) 用法同上
  substring(int a,int b) b参数应该为截取后的字符串长度
  上面string得用法不知是针对哪种语法
  针对的是c#
  --------------------------------------------------------
  在VB中,如上的substring用法是正确的
  -----------------------------------------------------以下为crazyghost_von更新--------------------------------------------
  以上函数的用法在JavaScript中也是正确的。
  -----------------------------------------------------------------------------------------------------------------------------------
  下面是在javascript中的用法.
  在JS中, 函数声明: stringObject.substring(start,stop)
  start是在原字符串检索的开始位置,stop是检索的终止位置,返回结果中不包括stop所指字符.
  例:
  
  上面返回字符串:"el";
  str.substring(1,1) //返回空;
  str.substring(1) //返回"ello world";
  还有此函数中会出现奇怪的现象,当出现str.substring(5,0);
  这又是怎么回事,不过返回的是"hello",
  str.substring(5,1) //返回"ello",截去了第一位,返回余下的.
  可见substring(start,end),可以有不同的说明,即start可以是要返回的长度,end是所要去掉的多少个字符(从首位开始).

  在JS中,substr(start,length),用得较方便.


String.Length
  Length 属性返回此实例中 Char 对象的个数(int),而不是 Unicode 字符个数。原因在于一个 Unicode 字符可能会用多个 Char 表示。使用 System.Globalization.StringInfo 类来处理每个 Unicode 字符而不是每个 Char。
  在某些语言(例如 C 和 C++)中,null 字符指示字符串的结尾。在 .NET Framework 中,null 字符可以嵌入到字符串中。当字符串包含一个或多个 null 字符时,这些空字符将包括在字符串的总长度中。

 

indexof

  String.IndexOf 方法 (Char, [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类,使用正则表达式对字符串进行匹配。
  [转贴]原信息URL:http://www.jiaonan.net/html/blog/1/23464.htm
  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 同上。


 

 

posted @ 2009-02-27 11:01  zxlin25  阅读(1618)  评论(0编辑  收藏  举报