Java StringBuffer deleteCharAt() 方法



java StringBuffer deleteCharAt() 方法用於移除此序列中指定位置處的字元。此序列將縮短一個字元。

在 Java 中,索引指的是當前序列的字元位置。基本上,charAt() 方法用於檢索指定索引處的字元。deleteCharAt() 方法接受一個整數引數,該引數儲存字元索引的值。如果索引值為負數或大於給定序列長度,則會丟擲異常。

語法

以下是 Java StringBuffer deleteCharAt() 方法的語法:

public StringBuffer deleteCharAt(int index)

引數

  • index - 要移除的字元的索引。

返回值

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

示例:從 StringBuffer 中刪除子字串

如果索引值非負且小於序列長度,則此方法返回給定序列的新子字串。

在以下示例中,我們使用值“Java lang package”建立 StringBuffer 類的一個物件。使用 delete() 方法,我們嘗試刪除指定索引 3處的字元。

public class StringBufferDemo {
   public static void main(String[] args) {
      //create an object of the StringBuffer
      StringBuffer sb = new StringBuffer("Java lang package");
      System.out.println("Before deletion the string is: " + sb);
      //initialize the index value
      int index = 3;
      System.out.println("The given index value is: " + index);
      //using the deleteCharAt() method
      System.out.println("After deletion the character at the specified index " + index + " index is: " + sb.deleteCharAt(index));
   }
}

輸出

執行上述程式後,將產生以下結果:

Before deletion the string is: Java lang package
The given index value is: 3
After deletion the character at the specified index 3 index is: Jav lang package

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

如果索引值大於給定序列長度,則此方法將丟擲StringIndexOutOfBoundsException

在此程式中,我們使用值“JavaProgramming”例項化StringBuffer 類。使用deleteCharAt() 方法,我們嘗試刪除指定索引 30處的字元,該值大於給定序列。

public class Delete {
   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 index value
         int index = 30;// value is greater than the sequence length
         System.out.println("The given index value is: " + index);
         //using the deleteCharAt() method
         System.out.println("After deletion rhe remaing string is: " + sb.deleteCharAt(index));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

以下是上述程式的輸出:

Before deletion the string is: JavaProgramming
The given index value is: 30
java.lang.StringIndexOutOfBoundsException: Index 30 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.outOfBoundsCheckIndex(Preconditions.java:106)
   at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
   at java.base/java.lang.String.checkIndex(String.java:4557)
   at java.base/java.lang.AbstractStringBuilder.deleteCharAt(AbstractStringBuilder.java:957)
   at java.base/java.lang.StringBuffer.deleteCharAt(StringBuffer.java:486)
   at Delete.main(Delete.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 15

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

如果索引值包含負值,則在呼叫此方法時,會丟擲StringIndexOutOfBoundsException

在以下示例中,我們建立一個值為“TutorialsPoint”StringBuffer。使用deleterCharAt() 方法,我們嘗試刪除指定索引 -1處的字元。

public class Demo {
   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 index value
         int index = -1;// holds the negative value
         System.out.println("The given index value is: " + index);
         //using the deleteCharAt() method
         System.out.println("After deletion rhe remaing string is: " + sb.deleteCharAt(index));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

上述程式產生以下結果:

Before deletion the string is: TutorialsPoint
The given index value is: -1
java.lang.StringIndexOutOfBoundsException: Index -1 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.outOfBoundsCheckIndex(Preconditions.java:106)
   at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
   at java.base/java.lang.String.checkIndex(String.java:4557)
   at java.base/java.lang.AbstractStringBuilder.deleteCharAt(AbstractStringBuilder.java:957)
   at java.base/java.lang.StringBuffer.deleteCharAt(StringBuffer.java:486)
   at Demo.main(Demo.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: Index -1 out of bounds for length 14
java_lang_stringbuffer.htm
廣告