- Java.lang Package classes
- 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 extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- 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
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.
package com.tutorialspoint.StringBuilder; 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
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.
package com.tutorialspoint.StringBuilder; 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, length 5 at java.base/java.lang.String.checkIndex(String.java:4563) at java.base/java.lang.AbstractStringBuilder.setCharAt(AbstractStringBuilder.java:533) at java.base/java.lang.StringBuilder.setCharAt(StringBuilder.java:91) at com.tutorialspoint.StringBuilder.SetChar.main(SetChar.java:13) Exception: java.lang.StringIndexOutOfBoundsException: index -1, length 5
Example
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.
package com.tutorialspoint.StringBuilder; 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, length 14 at java.base/java.lang.String.checkIndex(String.java:4563) at java.base/java.lang.AbstractStringBuilder.setCharAt(AbstractStringBuilder.java:533) at java.base/java.lang.StringBuilder.setCharAt(StringBuilder.java:91) at com.tutorialspoint.StringBuilder.SetChar.main(SetChar.java:13) Exception: java.lang.StringIndexOutOfBoundsException: index 20, length 14