string.substring()的使用注意事项

在Java开发中,substring()方法可能导致StringIndexOutOfBoundsException和NullPointerException。文章详细介绍了substring(int beginIndex, int endIndex)和substring(int beginIndex)可能遇到的越界、负数索引及空指针异常,并提出了相应的解决办法。此外,不恰当的substring使用还可能造成内存占用和性能损耗,特别是在创建新字符串时引用了原长字符串。建议开发者注意这些潜在问题以优化代码。" 80792605,7472828,Python 2.7 学习要点解析,"['Python', '基础知识', '编程语法']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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

 

如有不足,欢迎指出,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值