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()));
}
}
输出结果
结果分析
h | e | l | l | o | 空格 | w | o | r | d |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
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));
}
}
运行结果
结果分析
h | e | l | l | o | 空格 | w | o | r | d |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
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编程水平产生了积极的影响,使我在字符串处理方面更加灵活。