截取子字符串是字符串最常用的操作之一。Java的String类提供了两个截取子字符串的方法:
public String substring(int beginIndex);
public String substring(int beginIndex, int endIndex) ;
在JDK1.6,这两者的源代码如下:
public String substring(int beginIndex) {
return substring(beginIndex, count);
}
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > count) {
throw new StringIndexOutOfBoundsException(endIndex);
}
if (beginIndex > endIndex) {
throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
}
return ((beginIndex == 0) && (endIndex ==