目录
1、提出问题
2、测试Demo
3、源码:
顺序执行下列程序语句后,则b的值是()
String a="Hello";
String b=a.substring(0,2);
public class test_string_substring {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String string = "tomorrow will be better!";
System.out.println(string.substring(0, 5));
}
}
console:
tomor
/**
* Returns a new string that is a substring of this string. The
* substring begins at the specifiedbeginIndex
and
* extends to the character at indexendIndex - 1
.
* Thus the length of the substring isendIndex-beginIndex
.
*
* Examples:
*
* "hamburger".substring(4, 8) returns "urge"
* "smiles".substring(1, 5) returns "mile"
*
*
* @param beginIndex the beginning index, inclusive.
* @param endIndex the ending index, exclusive.
* @return the specified substring.
* @exception IndexOutOfBoundsException if the
*beginIndex
is negative, or
*endIndex
is larger than the length of
* thisString
object, or
*beginIndex
is larger than
*endIndex
.
*/
public String substring(int beginIndex, int endIndex) {
//1. 开始索引位置<0;抛出异常
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
//2. 结束索引位置>长度length,异常;value是String类的真实数据,本质是char[]数组
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
//3. 计算两个索引间的截取长度
int subLen = endIndex - beginIndex;
//4. 截取长度<0,异常
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
//5. 开始索引为0且结束索引为总长度,返回本身;否则 !!新建一个String类型数据,这里调用了构造函数:String(char value[], int offset, int count)
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}
String构造函数:其本质是使用了Arrays工具类的 Arrays.copyOfRange(value, offset, offset+count);
/**
* Allocates a new {@code String} that contains characters from a subarray
* of the character array argument. The {@code offset} argument is the
* index of the first character of the subarray and the {@code count}
* argument specifies the length of the subarray. The contents of the
* subarray are copied; subsequent modification of the character array does
* not affect the newly created string.
*
* @param value
* Array that is the source of characters
*
* @param offset
* The initial offset
*
* @param count
* The length
*
* @throws IndexOutOfBoundsException
* If the {@code offset} and {@code count} arguments index
* characters outside the bounds of the {@code value} array
*/
public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count);
}
Arrays工具类,复制源char数组的,从beginIndex 到 beginIndex + count 位置的元素;
/**
* Copies the specified range of the specified array into a new array.
* The initial index of the range (from) must lie between zero
* and original.length, inclusive. The value at
* original[from] is placed into the initial element of the copy
* (unless from == original.length or from == to).
* Values from subsequent elements in the original array are placed into
* subsequent elements in the copy. The final index of the range
* (to), which must be greater than or equal to from,
* may be greater than original.length, in which case
* '\\u000' is placed in all elements of the copy whose index is
* greater than or equal to original.length - from. The length
* of the returned array will be to - from.
*
* @param original the array from which a range is to be copied
* @param from the initial index of the range to be copied, inclusive
* @param to the final index of the range to be copied, exclusive.
* (This index may lie outside the array.)
* @return a new array containing the specified range from the original array,
* truncated or padded with null characters to obtain the required length
* @throws ArrayIndexOutOfBoundsException if {@code from < 0}
* or {@code from > original.length}
* @throws IllegalArgumentException if from > to
* @throws NullPointerException if original is null
* @since 1.6
*/
public static char[] copyOfRange(char[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
char[] copy = new char[newLength];
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
综上,substring()方法的结果分析完毕;