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
广告
© . All rights reserved.