- 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 setCharAt() 方法
**Java StringBuffer setCharAt()** 方法用於在 StringBuffer 物件的指定索引處新增/插入字元。**索引**指的是給定序列中字元的位置。我們作為索引引數傳遞的值必須大於或等於 0,且小於此序列的長度。
**setCharAt()** 方法接受兩個引數作為整數和字元,分別儲存**索引**和**ch**的值。如果索引值為負數,則會丟擲異常。
語法
以下是 **Java StringBuffer setCharAt()** 方法的語法:-
public void setCharAt(int index, char ch)
引數
- **index** - 這是 char 值的索引。
- **ch** - 這是新的字元。
返回值
此方法不返回值。
示例:在給定索引處設定字串中的字元
如果給定的索引值為**正數**,並且**小於字串長度**,則 setCharAt() 方法會在指定索引處設定字元。
在以下程式中,我們使用值**“Tutorix”**例項化**StringBuffer 類**。使用**setCharAt()** 方法,我們嘗試在指定索引**0**處設定字元**‘P’**。
public class SetChar {
public static void main(String[] args) {
//instantiate the StringBuffer class
StringBuffer sb = new StringBuffer("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”**建立**StringBuffer 類**的物件。使用**setCharAt()** 方法,我們嘗試在指定索引**-1**處設定字元**‘o’**。
public class SetChar {
public static void main(String[] args) {
try {
//create an object of the StringBuffer class
StringBuffer sb = new StringBuffer("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.StringBuffer.setCharAt(StringBuffer.java:299) at SetChar.main(SetChar.java:14) Exception: java.lang.StringIndexOutOfBoundsException: Index -1 out of bounds for length 5
示例:在給定索引處設定字串中的字元時遇到 IndexOutOfBoundsException
如果給定的索引值**大於字串長度**,此方法會丟擲**IndexOutOfBoundsException**。
在以下程式中,我們使用值**“TutorialsPoint”**例項化**StringBuffer 類**。然後,使用**setCharAt()** 方法,我們嘗試在指定索引**20**處設定字元**‘J’**,其值大於字串長度。
public class SetChar {
public static void main(String[] args) {
try {
//instantiate the StringBuffer class
StringBuffer sb = new StringBuffer("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.StringBuffer.setCharAt(StringBuffer.java:299) at SetChar.main(SetChar.java:14) Exception: java.lang.StringIndexOutOfBoundsException: Index 20 out of bounds for length 14