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


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


StringBuffer類的subSequence(int start,int end)方法是一種內置方法,用於返回位於此序列的索引start和end-1之間的字符的子序列。子序列以索引開頭的char值開始,以(end-1)的char值結束。返回的子序列的長度為end-start。因此,如果開始等於結束,則返回一個空的子序列。

用法:

public CharSequence subSequence?(int start, int end)

參數:
此方法接受兩個參數:


  1. startInteger類型值是指子序列的起始endex。
  2. end整數類型值是指子序列的最後一個endex。

返回值:
此方法返回範圍從start到end-1的指定子序列。
異常:
如果start或end為負,或者end大於length(),或者start大於end,則拋出IndexOutOfBoundsException。

以下程序說明了java.lang.StringBuffer.subSequence()方法:
示例1:

// Java program to demonstrate 
// the subSequence() Method. 
  
class GFG { 
  
    public static void main(String[] args) 
    { 
        // create a StringBuffer object 
        // with a String pass as parameter 
        StringBuffer str 
            = new StringBuffer("WelcomeGeeks"); 
  
        // print string 
        System.out.println("String contains = " + str); 
  
        // get subSequence bewteen index 0 to 7 
        // using subSequence() and print 
        System.out.println("SubSequence = "
                           + str.subSequence(0, 7)); 
    } 
}

輸出:

String length = 12 and contains = WelcomeGeeks
SubSequence = Welcome

示例2:

// Java program to demonstrate 
// the subSequence() 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"); 
  
        // print string 
        System.out.println("String contains = " + str); 
  
        // get subSequence bewteen index 0 to 7 
        // using subSequence() and print 
        System.out.println("SubSequence = "
                           + str.subSequence(7, 18)); 
    } 
}

輸出:

String contains = Indian Team Played Well
SubSequence = Team Played

示例3:當開始>結束時:

// Java program to demonstrate 
// Exception thrown by the subSequence() 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 subSequence bewteen index 0 to 7 
            // using subSequence() and print 
            System.out.println(str.subSequence(19, 18)); 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 
}

輸出:

java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.AbstractStringBuffer.substring(AbstractStringBuffer.java:935)
    at java.lang.StringBuffer.substring(StringBuffer.java:76)
    at java.lang.AbstractStringBuffer.subSequence(AbstractStringBuffer.java:912)
    at java.lang.StringBuffer.subSequence(StringBuffer.java:76)
    at GFG.main(File.java:16)

參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#subSequence(int,int)



相關用法


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