- 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 length)返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,一直到 length-1 位置处的字符串。
- 示例:
- "hamburger".substring(4,8) returns "urge"
- "smiles".substring(1,5) returns "mile"
- 参数:
- beginIndex - 开始处的索引(包括)。
- 返回:
- 指定的子字符串。
- 抛出:
- IndexOutOfBoundsException - 如果 beginIndex 为负,或length大于字符串长度。
- -----------------------------------------------------以下为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),用得较方便.
- //该片段来自于http://www.codesnippet.cn/detail/1908201513497.html
来源: http://www.codesnippet.cn/detail/1908201513497.html