Java基础知识整理(三),java基础知识整理
分享于 点击 38985 次 点评:116
Java基础知识整理(三),java基础知识整理
以前不知道在哪里看到过,介绍substring方法,这里简单总结一下。
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 == count)) ? this :
new String(offset + beginIndex, endIndex - beginIndex, value);
}
记得最开始的时候,老是分不清,怎么去判断,后来找到一个方法:
下标从第一个字符前开始,就很方便看出来了。
当然也可以使用包括开始字段,不包括结束字段的方法
"hello".substring(2) //llo
"hello".substring(3,5) //lo
相关文章
- 暂无相关文章
用户点评