Java StringBuilder setCharAt() 方法



Java StringBuilder setCharAt() 方法用於在 StringBuilder 物件的指定索引處新增/插入字元。索引指的是給定序列中字元的位置。我們作為索引引數傳遞的值必須大於或等於 0,並且小於此序列的長度。

setCharAt() 方法接受兩個引數作為整數和字元,分別儲存索引ch的值。如果索引值為負,則會丟擲異常。

語法

以下是Java StringBuilder setCharAt() 方法的語法:

public void setCharAt(int index, char ch)

引數

  • index - 這是 char 值的索引。
  • ch - 這是新字元。

返回值

此方法不返回值。

示例:在給定索引處設定字串中的字元

如果給定的索引值為正數,並且小於字串長度,則 setCharAt() 方法會在指定索引處設定字元。

在下面的程式中,我們使用值“Tutorix”例項化StringBuilder 類。使用setCharAt() 方法,我們嘗試在指定索引0處設定字元‘P’

public class SetChar {
   public static void main(String[] args) {
      //instantiate the StringBuilder class
      StringBuilder sb = new StringBuilder("Tutorix");
      System.out.println("The given string is: " + sb);
      //initialize the index and ch values
      int index = 0;
      char ch = 'P';
      System.out.println("The initialize index and ch values are: " + index + " and " + ch);
      //using the setCharAt() method
      sb.setCharAt(index, ch);
      System.out.println("After setting the character the string is: " + sb);
   }
}

輸出

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

The given string is: Tutorix
The initialize index and ch values are: 0 and P
After setting the character the string is: Putorix

示例:在給定索引處設定字串中的字元時遇到 IndexOutOfBoundsException

如果給定的索引值為負數,則 setCharAt() 方法會丟擲IndexOutOfBoundsException

在下面的程式中,我們使用值“Hello”建立StringBuilder 類的物件。使用setCharAt() 方法,我們嘗試在指定索引-1處設定字元‘o’

public class SetChar {
   public static void main(String[] args) {
      try {
         //create an object of the StringBuilder class
         StringBuilder sb = new StringBuilder("Hello");
         System.out.println("The given string is: " + sb);
         //initialize the index and ch values
         int index = -1;
         char ch = 'o';
         System.out.println("The initialize index and ch values are: " + index + " and " + ch);
         //using the setCharAt() method
         sb.setCharAt(index, ch);
         System.out.println("After setting the character the string is: " + sb);
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

以下是上述程式的輸出:

The given string is: Hello
The initialize index and ch values are: -1 and o
java.lang.StringIndexOutOfBoundsException: Index -1 out of bounds for length 5
   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.setCharAt(AbstractStringBuilder.java:522)
   at java.base/java.lang.StringBuilder.setCharAt(StringBuilder.java:299)
   at SetChar.main(SetChar.java:14)
Exception: java.lang.StringIndexOutOfBoundsException: Index -1 out of bounds for length 5

示例:在給定索引處設定字串中的字元時遇到 IndexOutOfBoundsException

如果給定的索引值大於字串長度,則此方法會丟擲IndexOutOfBoundsException

在下面的程式中,我們使用值“TutorialsPoint”例項化StringBuilder 類。然後,使用setCharAt() 方法,我們嘗試在指定索引20處設定字元‘J’,該值大於字串長度。

public class SetChar {
   public static void main(String[] args) {
      try {
         //instantiate the StringBuilder class
         StringBuilder sb = new StringBuilder("TutorialsPoint");
         System.out.println("The given string is: " + sb);
         //initialize the index and ch values
         int index = 20;
         char ch = 'j';
         System.out.println("The initialize index and ch values are: " + index + " and " + ch);
         //using the setCharAt() method
         sb.setCharAt(index, ch);
         System.out.println("After setting the character the string is: " + sb);
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

輸出

上述程式產生以下結果:

The given string is: TutorialsPoint
The initialize index and ch values are: 20 and j
java.lang.StringIndexOutOfBoundsException: Index 20 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.setCharAt(AbstractStringBuilder.java:522)
   at java.base/java.lang.StringBuilder.setCharAt(StringBuilder.java:299)
   at SetChar.main(SetChar.java:14)
Exception: java.lang.StringIndexOutOfBoundsException: Index 20 out of bounds for length 14
java_lang_stringbuilder.htm
廣告