在java开发中,难免会用到string.substring()方法,往往会在开发的过程中忽略其的一些潜在异常处理。
下面来分享一下我遇到的总结而来的注意事项。
string.substring(int beginIndex, int endIndex)
先上源码:
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0) && (endIndex == value.length)) ? this
: new String(value, beginIndex, subLen);
}
触发越界异常
一、截取字符个数小于定义的endIndex个数 的问题
java.lang.StringIndexOutOfBoundsException
案例:
String areaCode = "CHN".trim();
areaCode = areaCode.substring(0,4);
如果substring截取字符个数小于定义的endIndex个数,那么就会报java.lang.StringIndexOutOfBoundsException异常,也就是说,substring的任何操作都不允许超出被处理字符串的长度。
二、起始 beginIndex < 0 的问题
String areaCode = "CHN".trim();
String province = areaCode.substring(-1,2);
如果substring 中的 beginIndex 小于 0,那么也会触发java.lang.StringIndexOutOfBoundsException。
三、endIndex - beginIndex < 0 的问题
String areaCode = "CHN".trim();
String province = areaCode.substring(3,2);
如果 endIndex - beginIndex < 0,那么也会触发java.lang.StringIndexOutOfBoundsException。
四、截取的字符串为空
String areaCode = "";
String province = areaCode.substring(0,2);
如果截取的字符串为空"",一样会触发java.lang.StringIndexOutOfBoundsException。
NullPointerException
如果截取的字符串为null,则会抛出空指针异常。
String areaCode = null;
String province = areaCode.substring(0,2);
解决办法
针对以上会出现的情况,都要进行处理。
String areaCode = "CHN".trim();
//String areaCode = "440300".trim();
if (areaCode != null && areaCode != "") {
// 4 根据endIndex实际的长度进行修改
if (areaCode.length() >= 4) {
String province = areaCode.substring(0,4);
System.out.println(province);
}
}
String substring(int beginIndex)
先上源码:
public String substring(int beginIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}
一、如果 beginIndex < 0 ,那么会触发 StringIndexOutOfBoundsException;
String areaCode = "CHN".trim();
String substring = areaCode.substring(-1);
二、如果要截取的字符串长度小于 beginIndex,那么也会触发StringIndexOutOfBoundsException;
String areaCode = "CHN".trim();
String substring = areaCode.substring(4);
解决办法
String areaCode = "CHN".trim();
if (areaCode != null && areaCode != "") {
// 2 根据endIndex实际的长度进行修改
if (areaCode.length() >= 2) {
String province = areaCode.substring(2);
System.out.println(province);
}
}
以上这些方面,在开发中,应该都是我们应该要考虑的。
另外,substring的使用不当也会造成占用大量内存,性能损害问题。
String areaCode = "CHN".trim();
String province = areaCode.substring(2);
这个province将直接引用这个areaCode,而不仅仅是province的字符串,如果areaCode是很长的字符串,或者甚至是文本,那么province将间接引用areaCode对象,这显然容易造成内存巨大占用。特别是把province当做Map的key时,那么内存占用将会是巨大的。
正确做法:
String areaCode = "CHN".trim();
String province = new String(areaCode.substring(2));
关于 substring 内存问题,可以访问下面地址详情查看。
java的substring,split,trim容易出错——内存长期占用不回收: https://lvdccyb.iteye.com/blog/1849542
如有不足,欢迎指出,谢谢!