substring()方法是java String类的常用方法,作用是返回字符串的子字符串。
1.substring(int beginIndex)方法:
beginIndex是起始索引值(包括),从0开始。该方法将字符串从beginIndex位置开始截取,一直到字符串末尾。
示例:
public class test1 {
public static void main(String[] args) {
String test_str = "0123456789";
System.out.println(test_str.substring(3));
}
}
输出:
3456789
2.substring(int beginIndex, int endIndex)方法:
beginIndex是起始索引值(包括),从0开始,endIndex是结束索引值(不包括)。该方法将字符串从beginIndex位置开始截取到endIndex-1位置处结束。
示例:
public class test1 {
public static void main(String[] args) {
String test_str = "0123456789";
System.out.println(test_str.substring(3, 6));
}
}
输出:
345