Java String.substring(int, int) 的使用
public String substring(int beginIndex, int endIndex)
返回一個新字符串,它是此字符串的一個子字符串。該子字符串從指定的 beginIndex 處開始,一直到索引 endIndex - 1 處的字符。因此,該子字符串的長度爲 endIndex-beginIndex。
示例:
public static void main(String[] args) {
String lstr = "123456789";
//print "9"
System.out.println(lstr.length());
//print "2345678"
System.out.println(lstr.substring(1, lstr.length()-1));
//print "123456789"
System.out.println(lstr.substring(0, lstr.length()));
}
參數:
beginIndex:開始處的索引(包括)。
endIndex :結束處的索引(不包括)。
返回:
指定的子字符串。
拋出:
IndexOutOfBoundsException - 如果 beginIndex 爲負,或 endIndex 大於此 String 對象的長度,或 beginIndex 大於 endIndex。
注:String的Index從0開始
返回一個新字符串,它是此字符串的一個子字符串。該子字符串從指定的 beginIndex 處開始,一直到索引 endIndex - 1 處的字符。因此,該子字符串的長度爲 endIndex-beginIndex。
示例:
public static void main(String[] args) {
String lstr = "123456789";
//print "9"
System.out.println(lstr.length());
//print "2345678"
System.out.println(lstr.substring(1, lstr.length()-1));
//print "123456789"
System.out.println(lstr.substring(0, lstr.length()));
}
參數:
beginIndex:開始處的索引(包括)。
endIndex :結束處的索引(不包括)。
返回:
指定的子字符串。
拋出:
IndexOutOfBoundsException - 如果 beginIndex 爲負,或 endIndex 大於此 String 對象的長度,或 beginIndex 大於 endIndex。
注:String的Index從0開始
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.