substring()的用法
1.public String substring(int beginIndex)
2.public String substring(int beginIndex, int endIndex)
截取父字符串的某一部分,收获一个子字符串。
参数:
beginIndex:开始的索引,对应String数字中的开始位置,
endIdex: 截止的索引位置,对应String中的结束位置
1、取得的字符串长度为:endIndex - beginIndex;
2、从beginIndex开始取,到endIndex结束,从0开始数,其中不包括endIndex位置的字符(采用1时,截取开始索引到父字符串结尾)
public class Test1 {
public static void main(String[] args){
String str = "Hello World !";
String sub1 = str.substring(0, 3);
System.out.println(sub1);
String sub2 = str.substring(0, str.length());
System.out.println(sub2);
String sub3 = str.substring(4);
System.out.println(sub3);
}
}