在StringBuilder類中,根據傳遞給它的參數,有兩種類型的子字符串方法。
substring(int start)
StringBuilder類的substring(int start)方法是一種內置方法,用於從索引開頭返回一個子字符串,並擴展到此序列的結尾。此方法返回的字符串包含從舊序列的索引開始到結尾的所有字符。
用法:
public String substring(int start)
參數:此方法僅接受一個參數開始,即整數類型值是指子字符串的開始索引。
返回值:此方法返回位於舊序列開始到結束範圍內的子字符串。
異常:如果start小於零或大於此對象的長度,則此方法將引發StringIndexOutOfBoundsException。
以下示例程序旨在說明StringBuilder substring()方法:
示例1:
// Java program to demonstrate
// the substring() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder str
= new StringBuilder("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 StringBuilder object
// with a String pass as parameter
StringBuilder str
= new StringBuilder("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)
StringBuilder類的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。
以下示例程序旨在說明StringBuilder.substring()方法:
示例1:
// Java program to demonstrate
// the substring() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder str
= new StringBuilder("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 StringBuilder object
// with a String pass as parameter
StringBuilder str
= new StringBuilder("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
參考文獻:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#substring(int, int)
相關用法
- Java StringBuilder append()用法及代碼示例
- Java StringBuilder offsetByCodePoints()用法及代碼示例
- Java StringBuilder indexOf()用法及代碼示例
- Java StringBuilder appendCodePoint()用法及代碼示例
- Java StringBuilder lastIndexOf()用法及代碼示例
- Java StringBuilder toString()用法及代碼示例
- Java StringBuilder trimToSize()用法及代碼示例
- Java Matcher appendTail(StringBuilder)用法及代碼示例
- Java Matcher appendReplacement(StringBuilder, String)用法及代碼示例
- Java StringBuffer substring()用法及代碼示例
- Java StringBuilder codePointAt()用法及代碼示例
- Java StringBuilder codePointCount()用法及代碼示例
- Java StringBuilder charAt()用法及代碼示例
- Java StringBuilder ensureCapacity()用法及代碼示例
- Java StringBuilder length()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 StringBuilder substring() method in Java with examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。