VOOZH about

URL: https://www.geeksforgeeks.org/java/stringbuffer-setcharat-method-in-java-with-examples/

⇱ StringBuffer setCharAt() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

StringBuffer setCharAt() method in Java with Examples

Last Updated : 4 Dec, 2018
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 sequence. The index argument must be greater than or equal to 0, and less than the length of the String contained by StringBuffer object. Syntax:
public void setCharAt(int index, char ch)
Parameters: This method takes two parameters:
  • index: Integer type value which refers to the index of character to be set.
  • ch: Character type value which refers to the new char.
Returns: This method returns nothing. Exception: This method throws IndexOutOfBoundException if the index is negative or greater than length(). Below programs demonstrate the setCharAt() method of StringBuffer Class Example 1:
Output:
String = Geeks For Geeks
After setCharAt() String = Geeks F0r Geeks
Example 2: To demonstrate IndexOutOfBoundsException.
Output:
Exception:java.lang.StringIndexOutOfBoundsException: String index out of range: -1
References: https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#setCharAt(int, char)
Comment