StringBuilder類的subSequence(int start,int end)方法是一種內置方法,用於返回位於此序列的索引start和end-1之間的字符的子序列。子序列以索引開頭的char值開始,以(end-1)的char值結束。返回的子序列的長度為end-start。因此,如果開始等於結束,則返回一個空的子序列。
用法:
public CharSequence subSequence?(int start, int end)
參數:
此方法接受兩個參數:
- startInteger類型值是指子序列的起始endex。
- end整數類型值是指子序列的最後一個endex。
返回值:
此方法返回範圍從start到end-1的指定子序列。
異常:
如果start或end為負,或者end大於length(),或者start大於end,則拋出IndexOutOfBoundsException。
以下程序說明了java.lang.StringBuilder.subSequence()方法:
示例1:
// Java program to demonstrate
// the subSequence() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder str
= new StringBuilder("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 StringBuilder object
// with a String pass as parameter
StringBuilder str
= new StringBuilder("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 StringBuilder object
// with a String pass as parameter
StringBuilder str
= new StringBuilder("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.AbstractStringBuilder.substring(AbstractStringBuilder.java:935) at java.lang.StringBuilder.substring(StringBuilder.java:76) at java.lang.AbstractStringBuilder.subSequence(AbstractStringBuilder.java:912) at java.lang.StringBuilder.subSequence(StringBuilder.java:76) at GFG.main(File.java:16)
參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#subSequence(int,int)
相關用法
- Java StringBuilder setLength()用法及代碼示例
- Java StringBuilder reverse()用法及代碼示例
- Java StringBuilder setCharAt()用法及代碼示例
- Java StringBuilder Class用法及代碼示例
- Java StringBuilder codePointCount()用法及代碼示例
- Java StringBuilder delete()用法及代碼示例
- Java StringBuilder charAt()用法及代碼示例
- Java StringBuilder deleteCharAt()用法及代碼示例
- Java StringBuilder codePointAt()用法及代碼示例
- Java StringBuilder ensureCapacity()用法及代碼示例
- Java StringBuilder codePointBefore()用法及代碼示例
- Java StringBuilder capacity()用法及代碼示例
- Java StringBuilder length()用法及代碼示例
- Java StringBuilder getChars()用法及代碼示例
- Java StringBuilder replace()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 StringBuilder subSequence() in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。