- Java.lang 包類
- Java.lang - 首頁
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包附加內容
- Java.lang - 介面
- Java.lang - 錯誤
- Java.lang - 異常
- Java.lang 包有用資源
- Java.lang - 有用資源
- Java.lang - 討論
Java StringBuffer delete() 方法
Java 的StringBuffer delete() 方法用於從序列中移除子字串的元素。該方法從起始索引開始移除子字串,直到最後一個索引之前的元素;也就是說,起始索引是包含的,而結束索引是不包含的。
但是,如果起始索引小於結束索引或大於建立的序列長度,則該方法會丟擲 StringIndexOutOfBoundsException 異常。
注意 - 如果結束索引大於序列長度,則該方法會移除從起始索引到此序列末尾的所有字元。
語法
以下是 Java StringBuffer delete() 方法的語法。
public StringBuffer delete(int start, int end)
引數
- start - 這是起始索引,包含在內。
- end - 這是結束索引,不包含在內。
返回值
此方法返回此序列的子字串。
示例:從 StringBuffer 中刪除子字串
如果 startIndex 值大於 0 且endIndex 值小於序列長度,則返回此序列的子字串。
在下面的程式中,首先,我們使用值“Welcome to Tutorials Point”例項化StringBuffer 類。使用delete()方法,我們嘗試刪除給定範圍內的字串(startIndex = 5 和 endIndex = 10)。
public class StringBufferDemo {
public static void main(String[] args) {
//create a StringBuffer
StringBuffer sb = new StringBuffer("Welcome to Tutorials Point");
System.out.println("Before deletion the string is: " + sb);
//initialize the startInde and endIndex values
int startIndex = 11;
int endIndex = 21;
System.out.println("The startIndex and endIndex values are: " + startIndex + " and " + endIndex);
//using the delete() method
StringBuffer new_str = sb.delete(startIndex, endIndex);
System.out.println("After deletion the remaing string is: " + new_str);
}
}
輸出
執行上述程式後,將產生以下結果:
Before deletion the string is: Welcome to Tutorials Point The startIndex and endIndex values are: 11 and 21 After deletion the remaing string is: Welcome to Point
示例:在從 StringBuffer 刪除子字串時遇到 StringIndexOutOfBoundsException
如果startIndex 值大於 endIndex 值,此方法將丟擲StringIndexOutOfBoundsException。
在下面的示例中,我們使用值“JavaProgramming”建立一個StringBuffer物件。然後使用delete()方法,我們嘗試刪除給定範圍內的字串(其中 startIndex > endIndex)。
public class StringBufferDemo {
public static void main(String[] args) {
try {
//create an object of the StringBuffer
StringBuffer sb = new StringBuffer("JavaProgramming");
System.out.println("Before deletion the string is: " + sb);
//initialize the startIndex and endIndex values
int startIndex = 10;// greater than the endIndex value
int endIndex = 5;
System.out.println("The startIndex and endIndex values are: " + startIndex + " and " + endIndex);
//using the delete() method
System.out.println("After deletion rhe remaing string is: " + sb.delete(startIndex, endIndex));
} catch(IndexOutOfBoundsException e) {
e.printStackTrace();
System.out.println("Exception: " + e);
}
}
}
輸出
以下是上述程式的輸出:
Before deletion the string is: JavaProgramming The startIndex and endIndex values are: 10 and 5 java.lang.StringIndexOutOfBoundsException: Range [10, 5) out of bounds for length 15 at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55) at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112) at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349) at java.base/java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:904) at java.base/java.lang.StringBuffer.delete(StringBuffer.java:475) at StringBufferDemo.main(StringBufferDemo.java:12) Exception: java.lang.StringIndexOutOfBoundsException: Range [10, 5) out of bounds for length 15
示例:在從 StringBuffer 刪除子字串時遇到 IndexOutOfBoundsException
如果 startIndex 包含負值,則 delete() 方法將丟擲IndexOutOfBoundsException。
在此示例中,我們使用值“TutorialsPoint”建立一個StringBuffer。然後,我們嘗試使用delete()方法刪除給定範圍內的字串(其中 startIndex 為負值-2)。
public class StringBufferDemo {
public static void main(String[] args) {
try {
//create an object of the StringBuffer
StringBuffer sb = new StringBuffer("TutorialsPoint");
System.out.println("Before deletion the string is: " + sb);
//initialize the startIndex and endIndex values
int startIndex = -2;// holds negative value
int endIndex = 5;
System.out.println("The startIndex and endIndex values are: " + startIndex + " and " + endIndex);
//using the delete() method
System.out.println("After deletion rhe remaing string is: " + sb.delete(startIndex, endIndex));
} catch(IndexOutOfBoundsException e) {
e.printStackTrace();
System.out.println("Exception: " + e);
}
}
}
輸出
上述程式產生以下結果:
Before deletion the string is: TutorialsPoint The startIndex and endIndex values are: -2 and 5 java.lang.StringIndexOutOfBoundsException: Range [-2, 5) out of bounds for length 14 at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55) at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213) at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210) at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112) at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349) at java.base/java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:904) at java.base/java.lang.StringBuffer.delete(StringBuffer.java:475) at StringBufferDemo.main(StringBufferDemo.java:14) Exception: java.lang.StringIndexOutOfBoundsException: Range [-2, 5) out of bounds for length 14