在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
參考文獻:
- https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#substring(int, int)
- https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#substring(int)
相關用法
- Java StringBuffer getChars()用法及代碼示例
- Java StringBuffer deleteCharAt()用法及代碼示例
- Java StringBuffer codePointBefore()用法及代碼示例
- Java StringBuffer setCharAt()用法及代碼示例
- Java StringBuffer codePointCount()用法及代碼示例
- Java StringBuffer trimToSize()用法及代碼示例
- Java StringBuffer toString()用法及代碼示例
- Java StringBuffer codePointAt()用法及代碼示例
- Java StringBuffer ensureCapacity()用法及代碼示例
- Java StringBuffer offsetByCodePoints()用法及代碼示例
- Java StringBuffer appendCodePoint()用法及代碼示例
- Java StringBuffer lastIndexOf()用法及代碼示例
- Java StringBuffer append()用法及代碼示例
- Java StringBuffer reverse()用法及代碼示例
- Java StringBuffer replace()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 StringBuffer substring() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。