String类之substring--->查找某位置对应的字

时间:2023-03-08 21:43:24

以下方法都是java内置类String类的内置方法(不是构造方法哦,就是普通的方法),不需要我们写,直接拿过来用即可。

substring方法对应Api介绍

String类之substring--->查找某位置对应的字

 

查找字符串中的 从int beginIndex及其以后的字符串:substring(int beginIndex)

public class Demo {
public static void main(String[] args) {
String Str="MyNameIsDsh";
String substr=Str.substring(8);//截取字符串Str第8位之后的字符
System.out.println("截取后的新字符串是:"+substr);
}
}

截取后的新字符串是:Dsh

查找字符串中的 从int beginIndex-int endIndex之间的字符串:substring(int beginIndex,int endIndex)

public class Demo {
public static void main(String[] args) {
String Str="MyNameIsDsh";
String substr=Str.substring(8,10);//截取字符串Str第8-10位之间的字符串
System.out.println("截取后的新字符串是:"+substr);
}
}

截取后的新字符串是:Ds

如果超出查找字符串本身长度核报异常:String index out of range