indexof Substring

String.IndexOf 方法 (value, [startIndex], [count])

  报告指定字符在此实例中的第一个匹配项的索引。搜索从指定字符位置开始,并检查指定数量的字符位置。

  参数

  value

  要查找的 Unicode 字符。 对 value 的搜索区分大小写。

  startIndex(Int32)

  可选项,搜索起始位置。不设置则从0开始。

  count(Int32)

  可选项,要检查的字符位置数。

  返回值

  如果找到该字符,则为 value 的索引位置;否则如果未找到,则为 -1。

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 - 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所指字符.

  例:

  <script type="text/javascript">

  var str="Hello world!"

  document.write(str.substring(1,3));

  </script>

  上面返回字符串:"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),用得较方便.

原文地址:https://www.cnblogs.com/hateyoucode/p/1568283.html