在String中有兩個substring()函數,如下: 一:String.substring(int start) 參數: start:要截取位置的索引 返回: 從start開始到結束的字符串 例如:String str = "hello word!"; System.out.println(str.substring(1)); System.out.println(str.substring(3)); System.out.println(str.substring(6)); 將得到結果為: ello word! lo word! ord! 如果start大於字符串的長度將會拋出越界異常; 二:String.substring(int beginIndex, int endIndex) 參數: beginIndex 開始位置索引 endIndex 結束位置索引 返回: 從beginIndex位置到endIndex位置內的字符串 例如:String str = "hello word!"; System.out.println(str.substring(1,4)); System.out.println(str.substring(3,5)); System.out.println(str.substring(0,4)); 將得到結果為: ell lo hell 如果startIndex和endIndex其中有越界的將會拋出越界異常。