StringBuilder setCharAt() in Java with Examples Last Updated : 19 Aug, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The setCharAt(int index, char ch) method of StringBuilder class is used to set the character at the position index passed as ch. This method changes the old sequence to represents a new sequence which is identical to old sequence only difference is a new character ch is present at position index. The index argument must be greater than or equal to 0, and less than the length of the String contained by StringBUilder object. Syntax: public void setCharAt(int index, char ch) Parameters: This method accepts two parameters: index - Integer type value which refers to the index of character you want to set. ch - Character type value which refers to the new char. Returns: This method returns nothing. Exception: If the index is negative, greater than length() then IndexOutOfBoundsException. Below programs illustrate the java.lang.StringBuilder.setCharAt() method: Example 1: Java // Java program to demonstrate // the setCharAt() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("WelcomeGeeks"); // print string System.out.println("String = " + str.toString()); // set char at index 2 to 'L' str.setCharAt(2, 'L'); // print string System.out.println("After setCharAt() String = " + str.toString()); } } Output: String = WelcomeGeeks After setCharAt() String = WeLcomeGeeks Example 2: Java // Java program to demonstrate // the setCharAt() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("Tony Stark will die"); // print string System.out.println("String = " + str.toString()); // set char at index 9 to '1' str.setCharAt(9, '1'); // print string System.out.println("After setCharAt() String = " + str.toString()); } } Output: String = Tony Stark will die After setCharAt() String = Tony Star1 will die Example 3: When negative index is passed: Java // Java program to demonstrate // Exception thrown by the setCharAt() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("Tony Stark"); try { // pass index -15 str.setCharAt(-15, 'A'); } catch (Exception e) { e.printStackTrace(); } } } Output: java.lang.StringIndexOutOfBoundsException: String index out of range: -15 at java.lang.AbstractStringBuilder.setCharAt(AbstractStringBuilder.java:407) at java.lang.StringBuilder.setCharAt(StringBuilder.java:76) at GFG.main(File.java:16) References: https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#setCharAt(int, char) Comment More infoAdvertise with us Next Article StringBuilder toString() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Java-StringBuilder Practice Tags : Java Similar Reads StringBuilder length() in Java with Examples The length() method of StringBuilder class returns the number of character the StringBuilder object contains. The length of the sequence of characters currently represented by this StringBuilder object is returned by this method. Syntax: public int length() Return Value: This method returns length o 2 min read StringBuilder setLength() in Java with Examples The setLength(int newLength) method of StringBuilder is used to set the length of the character sequence equal to newLength.For every index k greater than 0 and less than newLength. If the newLength passed as argument is less than the old length, the old length is changed to the newLength.If the new 2 min read StringBuffer setCharAt() method in Java with Examples The setCharAt() method of StringBuffer class sets the character at the position index to character which is the value passed as parameter to method. This method returns a new sequence which is identical to old sequence only difference is a new character ch is present at position index in new sequenc 2 min read StringBuilder replace() in Java with Examples The replace(int start, int end, String str) method of StringBuilder class is used to replace the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified index start and extends to the character at index end - 1 or to the end of the se 3 min read StringBuilder toString() method in Java with Examples The toString() method of the StringBuilder class is the inbuilt method used to return a string representing the data contained by StringBuilder Object. A new String object is created and initialized to get the character sequence from this StringBuilder object and then String is returned by toString( 3 min read StringBuilder charAt() in Java with Examples The charAt(int index) method of StringBuilder Class is used to return the character at the specified index of String contained by StringBuilder Object. The index value should lie between 0 and length()-1. Syntax: public char charAt(int index) Parameters: This method accepts one int type parameter in 3 min read Like