StringBuilder類subSequence()方法 (StringBuilder Class subSequence() method)
subSequence() method is available in java.lang package.
subSequence()方法在java.lang包中可用。
subSequence() method is used to return the new set of a character sequence that is a subset of this sequence.
subSequence()方法用于返回字符序列的新集合,該字符序列是該序列的子集。
subSequence() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
subSequence()方法是一個非靜態方法,只能通過類對象訪問,如果嘗試使用類名稱訪問該方法,則會收到錯誤消息。
subSequence() method may throw an exception at the time of character sequence.
subSequence()方法在字符序列時可能會引發異常。
IndexOutOfBoundsException – This exception may throw when end_seq > length or when st_seq > end_seq or when st_seq or end_seq are less than 0.
IndexOutOfBoundsException-當end_seq> length或st_seq> end_seq或st_seq或end_seq小于0時,可能引發此異常。
Syntax:
句法:
public CharSequence subSequence(int st_seq, int end_seq);
Parameter(s):
參數:
int st_seq – represents the beginning index of this character sequence.
int st_seq –表示此字符序列的開始索引。
int end_seq – represents the ending index of this character sequence.
int end_seq –表示此字符序列的結束索引。
Return value:
返回值:
The return type of this method is CharSequence, it returns the given subsequence of this character sequence.
此方法的返回類型為CharSequence ,它返回此字符序列的給定子序列。
Example:
例:
// Java program to demonstrate the example
// of subSequence(int st_seq, int end_seq)
// method of StringBuilder
public class SubSequence {
public static void main(String[] args) {
// Creating an StringBuilder object
StringBuilder st_b = new StringBuilder("Java World ");
// Display st_b object
System.out.println("st_b = " + st_b);
// By using subSequence() method is to return the sequence
// from the given index 0 to index 3 (but it does not include
// last index i.e. 4)
CharSequence cs = st_b.subSequence(0, 4);
System.out.println("st_b.subSequence(0,4) = " + cs);
}
}
Output
輸出量
st_b = Java World
st_b.subSequence(0,4) = Java
翻譯自: https://www.includehelp.com/java/stringbuilder-subsequence-method-with-example.aspx