StringBuilder setCharAt() in Java with Examples

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:

  1. index – Integer type value which refers to the index of character you want to set.
  2. 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

String = WelcomeGeeks
After setCharAt() String = WeLcomeGeeks

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output:

String = Tony Stark will die
After setCharAt() String = Tony Star1 will die

Example 3: When negative index is passed:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
        }
    }
}

chevron_right


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)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.