String.subString()截取字符串,返回子字符串
首先自定義一個字符串用來測試
String str = "you do not like me";
1.substring(int beginIndex)
說明:指定起始索引,返回從索引值(包括)后的子字符串
System.out.println(str.substring(11)); //like me
2.substring(int beginIndex, int endIndex)
說明:指定一個起始索引,一個結束索引,返回子字符串
注意:返回的子字符串中,包括起始索引位置的值,不包括結束索引位置的值,即左閉右開
System.out.println(str.substring(0, 3) + str.substring(10)); //you like me
注意:
如果起始索引小於0,或者截取后的部分超過了字符串的長度,則拋出索引越界異常
System.out.println(str.substring(0, str.length()+1));