JDK6和JDK7中String类下的substring方法的代码对比(仅贴代码,未详述)
Posted linqingyang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDK6和JDK7中String类下的substring方法的代码对比(仅贴代码,未详述)相关的知识,希望对你有一定的参考价值。
jdk1.6版本String.java文件中关于substring的代码及描述
1 /**
2 * Returns a new string that is a substring of this string. The
3 * substring begins with the character at the specified index and
4 * extends to the end of this string. <p>
5 * Examples:
6 * <blockquote><pre>
7 * "unhappy".substring(2) returns "happy"
8 * "Harbison".substring(3) returns "bison"
9 * "emptiness".substring(9) returns "" (an empty string)
10 * </pre></blockquote>
11 *
12 * @param beginIndex the beginning index, inclusive.
13 * @return the specified substring.
14 * @exception IndexOutOfBoundsException if
15 * <code>beginIndex</code> is negative or larger than the
16 * length of this <code>String</code> object.
17 */
18 public String substring(int beginIndex) {
19 return substring(beginIndex, count);
20 }
21
22 /**
23 * Returns a new string that is a substring of this string. The
24 * substring begins at the specified <code>beginIndex</code> and
25 * extends to the character at index <code>endIndex - 1</code>.
26 * Thus the length of the substring is <code>endIndex-beginIndex</code>.
27 * <p>
28 * Examples:
29 * <blockquote><pre>
30 * "hamburger".substring(4, 8) returns "urge"
31 * "smiles".substring(1, 5) returns "mile"
32 * </pre></blockquote>
33 *
34 * @param beginIndex the beginning index, inclusive.
35 * @param endIndex the ending index, exclusive.
36 * @return the specified substring.
37 * @exception IndexOutOfBoundsException if the
38 * <code>beginIndex</code> is negative, or
39 * <code>endIndex</code> is larger than the length of
40 * this <code>String</code> object, or
41 * <code>beginIndex</code> is larger than
42 * <code>endIndex</code>.
43 */
44 public String substring(int beginIndex, int endIndex) {
45 if (beginIndex < 0) {
46 throw new StringIndexOutOfBoundsException(beginIndex);
47 }
48 if (endIndex > count) {
49 throw new StringIndexOutOfBoundsException(endIndex);
50 }
51 if (beginIndex > endIndex) {
52 throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
53 }
54 return ((beginIndex == 0) && (endIndex == count)) ? this :
55 new String(offset + beginIndex, endIndex - beginIndex, value);
56 }
jdk1.7版本String.java文件中关于substring的代码及描述
1 /**
2 * Returns a new string that is a substring of this string. The
3 * substring begins with the character at the specified index and
4 * extends to the end of this string. <p>
5 * Examples:
6 * <blockquote><pre>
7 * "unhappy".substring(2) returns "happy"
8 * "Harbison".substring(3) returns "bison"
9 * "emptiness".substring(9) returns "" (an empty string)
10 * </pre></blockquote>
11 *
12 * @param beginIndex the beginning index, inclusive.
13 * @return the specified substring.
14 * @exception IndexOutOfBoundsException if
15 * <code>beginIndex</code> is negative or larger than the
16 * length of this <code>String</code> object.
17 */
18 public String substring(int beginIndex) {
19 if (beginIndex < 0) {
20 throw new StringIndexOutOfBoundsException(beginIndex);
21 }
22 int subLen = value.length - beginIndex;
23 if (subLen < 0) {
24 throw new StringIndexOutOfBoundsException(subLen);
25 }
26 return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
27 }
28
29 /**
30 * Returns a new string that is a substring of this string. The
31 * substring begins at the specified <code>beginIndex</code> and
32 * extends to the character at index <code>endIndex - 1</code>.
33 * Thus the length of the substring is <code>endIndex-beginIndex</code>.
34 * <p>
35 * Examples:
36 * <blockquote><pre>
37 * "hamburger".substring(4, 8) returns "urge"
38 * "smiles".substring(1, 5) returns "mile"
39 * </pre></blockquote>
40 *
41 * @param beginIndex the beginning index, inclusive.
42 * @param endIndex the ending index, exclusive.
43 * @return the specified substring.
44 * @exception IndexOutOfBoundsException if the
45 * <code>beginIndex</code> is negative, or
46 * <code>endIndex</code> is larger than the length of
47 * this <code>String</code> object, or
48 * <code>beginIndex</code> is larger than
49 * <code>endIndex</code>.
50 */
51 public String substring(int beginIndex, int endIndex) {
52 if (beginIndex < 0) {
53 throw new StringIndexOutOfBoundsException(beginIndex);
54 }
55 if (endIndex > value.length) {
56 throw new StringIndexOutOfBoundsException(endIndex);
57 }
58 int subLen = endIndex - beginIndex;
59 if (subLen < 0) {
60 throw new StringIndexOutOfBoundsException(subLen);
61 }
62 return ((beginIndex == 0) && (endIndex == value.length)) ? this
63 : new String(value, beginIndex, subLen);
64 }
点我,免费下载1.6、1.7版本的String类,仔细查看substring方法的异同。
链接:https://pan.baidu.com/s/13PQPcdeiD1J3Cj4VDJov5A
提取码:pg63
以上是关于JDK6和JDK7中String类下的substring方法的代码对比(仅贴代码,未详述)的主要内容,如果未能解决你的问题,请参考以下文章
在JDK 6和JDK 7的substring()方法的区别?