JAVA substring() 方法

substring() 方法返回字符串的子字符串。

第一种语法

public String substring(int beginIndex);

假设字符串长度为n,返回由beginIndex到n-1的字符组成的字符串,下标从0开始。

实例

public class Test{
	
    
    public static void main(String[] args) {
		// TODO Auto-generated method stub
    	String Str = new String("hello word");
    	 
        System.out.print("返回值1:" );
        System.out.println(Str.substring(0) );
        System.out.print("返回值2:" );
        System.out.println(Str.substring(4));
        System.out.print("返回值3:" );
        System.out.println(Str.substring(Str.length()));
    }   	
}

输出结果

结果分析

hello空格word
0123456789

Str.substring(0)返回的是0-9的字符组成的字符串,即h到d,相当于返回本身

Str.substring(4)返回的是4-9的字符组成的字符串,即o-d

Str.substring(Str.length())返回的是空串

查看源码,该方法在String类

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=Str.length(),那么返回的是new String(value, beginIndex, subLen);

value是该String类的成员变量,查看该类的value成员变量可以发现,value是个字符数组,保存该字符串对应的字符数组。subLen=value.length - beginIndex也就是0

查看new String(value, beginIndex, subLen)对应的public String(char value[], int offset, int count) 方法,subLen=0,beginIndex=Str.length(),对应offset=Str.length(),count=0,进入到下图红色部分,所以返回的是空串。

 

注意

String(char value[], int offset, int count)返回的是[offset,count]的字符组成的字符串,下标从0开始 

public class Test{
    public static void main(String[] args) {
		// TODO Auto-generated method stub
    	String Str = new String("hello word");
    	char value[]=Str.toCharArray();
        String a=new String(value,1,3);
        System.out.println("a:"+a);  
    }
}

输出

 

第二种语法

public String substring(int beginIndex, int endIndex);

假设字符串长度为n,返回由beginIndex到endIndex-1的字符组成的字符串,下标从0开始。相当于[beginIndex,endIndex);

实例

public class Test{
	
    
    public static void main(String[] args) {
		// TODO Auto-generated method stub
    	String Str = new String("hello word");
        System.out.print("返回值1:" );
        System.out.println(Str.substring(0,Str.length()) );
        System.out.print("返回值2:" );
        System.out.println(Str.substring(4,7));
        System.out.print("返回值3:" );
        System.out.println(Str.substring(2,2));
        
    }
}

运行结果

结果分析

hello空格word
0123456789

Str.substring(0,Str.length()) 返回的是[0,10)字符组成的字符串即本身

Str.substring(4,7)返回的是[4,7)字符组成的字符串即o w

Str.substring(2,2),当beginIndex和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);
    }

如果beginIndex=endIndex,那么返回new String(value, beginIndex, subLen),后面同第一种语法返回空串分析一样

总结

调用str.substring(beginIndex)返回[beginIndex,str.length())的字符串,下标从0开始

调用str.substring(beginIndex,endIndex)返回[beginIndex,endlindex)的字符串,下标从0开始

通过学习Java中的substring()方法,我对字符串处理的灵活性有了明显提升。掌握了这一方法后,我能够更方便地截取和操作字符串,尤其是在处理文本信息、解析日期或提取关键信息时。学习过程中,我发现通过指定起始和结束索引,能够精确地获取所需的子字符串,这在实际编码中非常实用。

此外,对substring()方法的理解也拓展了我对Java字符串处理的整体认识,使我能够更高效地解决实际编程中遇到的问题。通过编写简单的代码示例,我了解了方法的用法和不同参数的作用,从而提高了我的编程技能。这一学习经历对我个人的Java编程水平产生了积极的影响,使我在字符串处理方面更加灵活。

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值