java.lang.StringBuffer.delete()是Java中的內置方法,用於刪除或刪除此序列的子字符串中的字符。子字符串從指定的索引start_point開始,並擴展到在索引end_point處的字符。
用法:
public StringBuffer delete(int start_point, int end_point)
參數:該方法接受兩個整數類型的參數:
start_point–這是指起始索引,並包含在計數中。
end_point–這指的是結束索引,不包括在計數中。
返回值:在刪除參數中提到的範圍所形成的子字符串後,該方法將返回字符串。
異常:如果start_point為負,大於length(),或者大於end_point,則發生StringIndexOutOfBoundsException。
例子:
Input: String = "Apple" start_point = 2 end_point = 4 Output: Ape Input: String = "GeeksforGeeks" start_point = 2 end_point = 7 Output: GerGeeks
以下程序說明了java.lang.StringBuffer.delete()方法:
示例1:
// Java praogram to illustrate the
// java.lang.StringBuffer.delete()
import java.lang.*;
public class geeks {
public static void main(String[] args)
{
StringBuffer sbf = new StringBuffer("Geeksforgeeks");
System.out.println("string buffer = " + sbf);
// Deleting characters from index 2 to 7
sbf.delete(6, 8);
System.out.println("After deletion string buffer is = " + sbf);
}
}
輸出:
string buffer = Geeksforgeeks After deletion string buffer is = Geeksfgeeks
示例2:
// Java praogram to illustrate the
// java.lang.StringBuffer.delete()
import java.lang.*;
public class geeks {
public static void main(String[] args)
{
StringBuffer sbf = new StringBuffer("Welcome to Geeksforgeeks");
System.out.println("string buffer = " + sbf);
// deleting characters from index 5 to index 9
sbf.delete(5, 9);
System.out.println("After deletion string buffer is = " + sbf);
}
}
輸出:
string buffer = Welcome to Geeksforgeeks After deletion string buffer is = Welcoo Geeksforgeeks
示例3:此處索引為負。
// Java praogram to illustrate the
// java.lang.StringBuffer.delete()
import java.lang.*;
public class geeks {
public static void main(String[] args)
{
StringBuffer sbf = new StringBuffer("Welcome to Geeksforgeeks");
System.out.println("string buffer = " + sbf);
sbf.delete(-5, 9);
System.out.println("After deletion string buffer is = " + sbf);
}
}
輸出:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -5 at java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:756) at java.lang.StringBuffer.delete(StringBuffer.java:430) at geeks.main(geeks.java:13)
示例4:此處沒有索引。
// Java praogram to illustrate the
// java.lang.StringBuffer.delete()
import java.lang.*;
public class geeks {
public static void main(String[] args)
{
StringBuffer sbf = new StringBuffer("Welcome to Geeksforgeeks");
System.out.println("string buffer = " + sbf);
sbf.delete(99, 109);
System.out.println("After deletion string buffer is = " + sbf);
}
}
輸出:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException at java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:760) at java.lang.StringBuffer.delete(StringBuffer.java:430) at geeks.main(geeks.java:13)
相關用法
- Java StringBuffer indexOf()用法及代碼示例
- Java StringBuffer appendCodePoint()用法及代碼示例
- Java StringBuffer lastIndexOf()用法及代碼示例
- Java StringBuffer deleteCharAt()用法及代碼示例
- Java StringBuffer reverse()用法及代碼示例
- Java StringBuffer append()用法及代碼示例
- Java StringBuffer replace()用法及代碼示例
- Java StringBuffer substring()用法及代碼示例
- Java StringBuffer ensureCapacity()用法及代碼示例
- Java StringBuffer codePointBefore()用法及代碼示例
- Java StringBuffer offsetByCodePoints()用法及代碼示例
- Java StringBuffer setCharAt()用法及代碼示例
- Java StringBuffer trimToSize()用法及代碼示例
- Java StringBuffer getChars()用法及代碼示例
- Java StringBuffer toString()用法及代碼示例
注:本文由純淨天空篩選整理自ankita_chowrasia大神的英文原創作品 StringBuffer delete() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。