StringBuilder類的delete(int start,int end)方法從StringBuilder包含的String中刪除從索引start到index end-1的字符。此方法使用兩個索引作為參數,第一個開始代表第一個字符的索引,結束索引代表要從StringBuilder包含的String中刪除的子字符串的最後一個字符之後的索引,並將剩餘的String作為StringBuilder對象返回。
用法:
public StringBuilder delete(int start, int end)
參數:此方法接受兩個參數:
- start:子字符串的第一個字符的索引。
- end:子字符串最後一個字符之後的索引。
返回值:刪除子字符串後,此方法返回此StringBuilder對象。
異常:如果start小於零,或者start大於String的長度,或者start大於end,則此方法引發StringIndexOutOfBoundsException。
下麵的程序演示StringBuilder類的delete()方法:
示例1:
// Java program to demonstrate
// the delete() 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("Before removal String = "
+ str.toString());
// remove the substring from index 2 to 8
StringBuilder afterRemoval = str.delete(2, 8);
// print string after removal of substring
System.out.println("After removal String = "
+ afterRemoval.toString());
}
}
輸出:
Before removal String = WelcomeGeeks After removal String = Weeeks
示例2:
// Java program to demonstrate
// the delete() 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("Before removal String = "
+ str.toString());
// remove the substring from index 8 to 8
StringBuilder afterRemoval = str.delete(8, 8);
// start equal to end so no change in string
// print string after removal of substring
System.out.println("After removal of SubString"
+ " start=8 to end=8"
+ " String becomes => "
+ afterRemoval.toString());
// remove the substring from index 1 to 8
afterRemoval = str.delete(1, 8);
// print string after removal of substring
System.out.println("After removal of SubString"
+ " start=1 to end=8"
+ " String becomes => "
+ afterRemoval.toString());
}
}
輸出:
Before removal String = GeeksforGeeks After removal of SubString start=8 to end=8 String becomes => GeeksforGeeks After removal of SubString start=1 to end=8 String becomes => GGeeks
示例3:演示IndexOutOfBoundException
// Java program to demonstrate
// exception thrown by the delete() Method.
class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder
str
= new StringBuilder("GeeksforGeeks");
try {
// make start greater than end
StringBuilder afterRemoval = str.delete(7, 4);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
輸出:
Exception: java.lang.StringIndexOutOfBoundsException
參考:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#delete(int, int)
相關用法
- Java StringBuilder reverse()用法及代碼示例
- Java StringBuilder setCharAt()用法及代碼示例
- Java StringBuilder replace()用法及代碼示例
- Java StringBuilder codePointCount()用法及代碼示例
- Java StringBuilder charAt()用法及代碼示例
- Java StringBuilder codePointAt()用法及代碼示例
- Java StringBuilder subSequence()用法及代碼示例
- Java StringBuilder setLength()用法及代碼示例
- Java StringBuilder ensureCapacity()用法及代碼示例
- Java StringBuilder length()用法及代碼示例
- Java StringBuilder capacity()用法及代碼示例
- Java StringBuilder getChars()用法及代碼示例
- Java StringBuilder codePointBefore()用法及代碼示例
- Java StringBuilder Class用法及代碼示例
- Java StringBuilder deleteCharAt()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 StringBuilder delete() in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。