Java StringBuilder delete() 方法



Java 的 StringBuilder delete() 方法用於從序列中移除子字串的元素。該方法從起始索引開始移除子字串,直到最後一個索引之前的元素;也就是說,起始索引是包含的,而結束索引是不包含的。

但是,如果起始索引小於結束索引或大於建立的序列的長度,則該方法會丟擲 StringIndexOutOfBoundsException 異常。

注意 - 如果結束索引大於序列的長度,則該方法會移除從起始索引到該序列末尾的所有字元。

語法

以下是 Java StringBuilder delete() 方法的語法。

public StringBuilder delete(int start, int end)

引數

  • start - 這是起始索引,包含在內。
  • end - 這是結束索引,不包含在內。

返回值

此方法返回此序列的子字串。

示例:從 StringBuilder 中刪除子字串

如果 startIndex 值 大於 0endIndex 值小於序列長度,則它返回此序列的子字串。

在下面的程式中,首先,我們使用值 “Welcome to Tutorials Point” 例項化 StringBuilder 類。使用 delete() 方法,我們嘗試刪除給定範圍內的字串 (startIndex = 5 和 endIndex = 10)

public class StringBuilderDemo {
   public static void main(String[] args) {
      //create a StringBuilder
      StringBuilder sb = new StringBuilder("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
      StringBuilder 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

示例:在從 StringBuilder 刪除子字串時遇到 StringIndexOutOfBoundsException

如果 startIndex 值大於 endIndex 值,則此方法會丟擲 StringIndexOutOfBoundsException 異常。

在下面的示例中,我們使用值 “JavaProgramming” 建立一個 StringBuilder 物件。然後使用 delete() 方法,我們嘗試刪除給定範圍內的字串(其中 startIndex > endIndex)。

public class StringBuilderDemo {
   public static void main(String[] args) {
      try {
         //create an object of the StringBuilder
         StringBuilder sb = new StringBuilder("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.StringBuilder.delete(StringBuilder.java:475)
   at StringBuilderDemo.main(StringBuilderDemo.java:12)
Exception: java.lang.StringIndexOutOfBoundsException: Range [10, 5) out of bounds for length 15

示例:在從 StringBuilder 刪除子字串時遇到 IndexOutOfBoundsException

如果 startIndex 包含 負值,則 delete() 方法會丟擲 IndexOutOfBoundsException 異常。

在這個示例中,我們使用值 “TutorialsPoint” 建立一個 StringBuilder。然後,我們嘗試使用 delete() 方法刪除給定範圍內的字串(其中 startIndex 具有負值 -2)。

public class StringBuilderDemo {
   public static void main(String[] args) {
      try {
         //create an object of the StringBuilder
         StringBuilder sb = new StringBuilder("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.StringBuilder.delete(StringBuilder.java:475)
   at StringBuilderDemo.main(StringBuilderDemo.java:14)
Exception: java.lang.StringIndexOutOfBoundsException: Range [-2, 5) out of bounds for length 14
java_lang_stringbuilder.htm
廣告