
- Java.lang - Home
- 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 Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java StringBuilder setCharAt() Method
The Java StringBuilder setCharAt() method, is used to add/insert the character at the specified index in a StringBuilder object. The index refer to the character position in the given sequence. The value we pass as index argument must be greater than or equal to 0, and less than the length of this sequence.
The setCharAt() method accepts two parameters as an integer and character that holds the value of the index and ch. It throws an exception if the index value is negative.
Syntax
Following is the syntax of the Java StringBuilder setCharAt() method −
public void setCharAt(int index, char ch)
Parameters
- index − This is the index of the char value.
- ch − This is the new character.
Return Value
This method does not return any value.
Example: Setting a Char in String at given Index
If the given index value is positive, and less than the string length, the setCharAt() method, sets the character at the specified index.
In the following program, we are instantiating the StringBuilder class with the value Tutorix. Using the setCharAt() method, we are trying to set the character P at the specified index of 0.
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); } }
Output
On executing the above program, it will produce the following result −
The given string is: Tutorix The initialize index and ch values are: 0 and P After setting the character the string is: Putorix
Example: Facing IndexOutOfBoundsException while Setting a Char in String at given Index
If the given index value is negative, the setCharAt() method throws the IndexOutOfBoundsException.
In the following program, we are creating an object of the StringBuilder class with the value Hello. Using the setCharAt() method, we are trying to set the character o at the specified index of -1.
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); } } }
Output
Following is the output of the above program −
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
Example: Facing IndexOutOfBoundsException while Setting a Char in String at given Index
If the given index value is greater than the string length,this method throws IndexOutOfBoundsException.
In the following program, we are instantiating the StringBuilder class with the value TutorialsPoint. Then, using the setCharAt() method, we are trying to set the character J at the specified index 20 whose value is greater than the string length.
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); } } }
Output
The above program, produces the following results −
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