StringBuffer setCharAt() method in Java with Examples Last Updated : 04 Dec, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Java // Java program to demonstrate // the setCharAt() Method. class GFG { public static void main(String[] args) { // create a StringBuffer object // with a String pass as parameter StringBuffer str = new StringBuffer("Geeks For Geeks"); // print string System.out.println("String = " + str.toString()); // set char at index 4 to '0' str.setCharAt(7, '0'); // print string System.out.println("After setCharAt() String = " + str.toString()); } } Output: String = Geeks For Geeks After setCharAt() String = Geeks F0r Geeks Example 2: To demonstrate IndexOutOfBoundsException. Java // Java program to demonstrate // Exception thrown by the setCharAt() Method. class GFG { public static void main(String[] args) { // create a StringBuffer object // with a String pass as parameter StringBuffer str = new StringBuffer("Geeks for Geeks"); try { // pass index -1 str.setCharAt(-1, 'T'); } catch (Exception e) { System.out.println("Exception:" + e); } } } 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 More infoAdvertise with us Next Article StringBuffer setCharAt() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java java-basics Java-lang package Java-Functions java-StringBuffer +1 More Practice Tags : Java Similar Reads StringBuffer reverse() Method in Java with Examples The Java.lang.StringBuffer.reverse() is an inbuilt method that is used to reverse the characters in the StringBuffer. The method causes this character sequence to be replaced by the reverse of the sequence. Syntax: public StringBuffer reverse() Parameters: NA Return Value: The method returns the Str 2 min read StringBuffer offsetByCodePoints() method in Java with Examples The offsetByCodePoints() method of StringBuffer class returns the index within this String contained by StringBuffer that is offset from the index passed as parameter by codePointOffset code points. Unpaired surrogates lies between index and codePointOffset count as one code point each. Syntax: publ 2 min read StringBuffer append() Method in Java with Examples Pre-requisite: StringBuffer class in Java The java.lang.StringBuffer.append() method is used to append the string representation of some argument to the sequence. There are 13 ways/forms in which the append() method can be used: StringBuffer append(boolean a) :The java.lang.StringBuffer.append(boole 13 min read StringBuffer delete() Method in Java with Examples The java.lang.StringBuffer.delete() is an inbuilt method in Java which is used to remove or delete the characters in a substring of this sequence. The substring starts at a specified index start_point and extends to the character at the index end_point. Syntax : public StringBuffer delete(int start_ 3 min read StringBuffer getChars() method in Java with Examples The getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method of StringBuffer class copies the characters starting at the index:srcBegin to index:srcEnd-1 from actual sequence into an array of char passed as parameter to function. The characters are copied into the array of char dst[] star 3 min read Like