當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java StringBuffer substring()用法及代碼示例


在StringBuffer類中,取決於傳遞給它的參數,有兩種類型的子字符串方法。

substring(int start)

StringBuffer類的substring(int start)方法是一種內置方法,用於從索引開頭返回一個子字符串,並擴展到此序列的結尾。此方法返回的字符串包含從舊序列的索引開始到結尾的所有字符。

用法:


public String substring(int start)

參數:此方法僅接受一個參數開始,即整數類型值是指子字符串的開始索引。

返回值:此方法返回位於舊序列開始到結束範圍內的子字符串。

異常:如果start小於零或大於此對象的長度,則此方法將引發StringIndexOutOfBoundsException。

以下示例程序旨在說明StringBuffer substring()方法:

示例1:

// Java program to demonstrate 
// the substring() Method. 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a StringBuffer object 
        // with a String pass as parameter 
        StringBuffer str 
            = new StringBuffer("GeeksForGeeks"); 
  
        // print string 
        System.out.println("String contains = "
                           + str); 
  
        // get substring start from index 5 
        // using substring() and print results 
        System.out.println("SubSequence = "
                           + str.substring(5)); 
    } 
}
輸出:
String contains = GeeksForGeeks
SubSequence = ForGeeks

示例2:演示StringIndexOutOfBoundsException

// Java program to demonstrate 
// Exception thrown by the substring() Method. 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a StringBuffer object 
        // with a String pass as parameter 
        StringBuffer str 
            = new StringBuffer("Indian Team Played Well"); 
  
        try { 
            // get substring starts from index -7 
            // using substring() and print 
            str.substring(-7); 
        } 
  
        catch (Exception e) { 
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -7

substring(int start, int end)

StringBuffer類的substring(int start,int end)方法是一種內置方法,用於從索引start返回子字符串start並擴展到此序列的index end-1。此方法返回的字符串包含從舊序列的索引開始到索引結束1的所有字符。

用法:


public String substring(int start)

參數:此方法接受兩個參數start,即Integer類型值引用子字符串的開始索引和end,後者也是Integer類型值引用子字符串的結束索引。

返回值:此方法返回位於舊序列索引開始到索引結束1的範圍內的子字符串。

異常:如果start或end為負或大於length(),或者start大於end,則此方法引發StringIndexOutOfBoundsException。

以下示例程序旨在說明StringBuffer.substring()方法:

示例1:

// Java program to demonstrate 
// the substring() Method. 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a StringBuffer object 
        // with a String pass as parameter 
        StringBuffer str 
            = new StringBuffer("GeeksForGeeks"); 
  
        // print string 
        System.out.println("String contains = " + str); 
  
        // get substring start from index 5 to index 8 
        // using substring() and print results 
        System.out.println("SubSequence = "
                           + str.substring(5, 8)); 
    } 
}
輸出:
String contains = GeeksForGeeks
SubSequence = For

示例2:演示StringIndexOutOfBoundsException

// Java program to demonstrate 
// Exception thrown by the substring() Method. 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // create a StringBuffer object 
        // with a String pass as parameter 
        StringBuffer str 
            = new StringBuffer("Indian Team Played Well"); 
  
        try { 
            // get substring starts from index 7 
            // and end at index 3 
            // using substring() and print 
            str.substring(9, 3); 
        } 
  
        catch (Exception e) { 
  
            System.out.println("Exception: " + e); 
        } 
    } 
}
輸出:
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -6

參考文獻



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 StringBuffer substring() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。