StringBuffer getChars() method in Java with Examples Last Updated : 17 Nov, 2022 Comments Improve Suggest changes Like Article Like Report 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[] starting at index:dstBegin and ending at index:dstbegin + (srcEnd-srcBegin) – 1.The first character to be copied to array is at index srcBegin and the last character to be copied is at index srcEnd-1.The total number of characters to be copied is equal to srcEnd-srcBegin. Syntax: public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) Parameters: This method takes four parameters: srcBegin: int value represents index on which the copying is to be started.srcEnd: int value represents index on which the copying is to be stopped.dst: array of char represents the array to copy the data into.dstBegin: int value represents index of dst array to start pasting the copied data. Returns: This method returns nothing. Exception: This method throws StringIndexOutOfBoundsException if: srcBegin < 0dstBegin < 0srcBegin > srcEndsrcEnd > this.length()dstBegin+srcEnd-srcBegin > dst.length Below programs demonstrate the getChars() method of StringBuffer Class: Example 1: Java // Java program to demonstrate // the getChars() Method. import java.util.*; 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()); // create a char Array char[] array = new char[15]; // initialize all character to .(dot). Arrays.fill(array, '.'); // get char from index 0 to 9 // and store in array start index 3 str.getChars(0, 9, array, 1); // print char array after operation System.out.print("char array contains : "); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } } Output:String = Geeks For Geeks char array contains : . G e e k s F o r . . . . . Example 2: To demonstrate StringIndexOutOfBoundsException. Java // Java program to demonstrate // exception thrown by the getChars() Method. import java.util.*; class GFG { public static void main(String[] args) { // create a StringBuffer object // with a String pass as parameter StringBuffer str = new StringBuffer("Contribute Geeks"); // create a char Array char[] array = new char[10]; // initialize all character to $(dollar sign). Arrays.fill(array, '$'); try { // if start is greater then end str.getChars(2, 1, array, 0); } catch (Exception e) { System.out.println("Exception: " + e); } } } Output:Exception: java.lang.StringIndexOutOfBoundsException: srcBegin > srcEnd References: https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#getChars(int, int, char%5B%5D, int) Comment More infoAdvertise with us Next Article StringBuffer getChars() 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 StringBuilder getChars() in Java with Examples The getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method of StringBuilder class copies the characters starting at the given index:srcBegin to index:srcEnd-1 from String contained by StringBuilder into an array of char passed as parameter to function. The characters are copied from Str 3 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 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 toString() method in Java with Examples The toString() method of StringBuffer class is the inbuilt method used to returns a string representing the data contained by StringBuffer Object. A new String object is created and initialized to get the character sequence from this StringBuffer object and then String is returned by toString(). Sub 2 min read Java String getChars() with examples The java string getChars() method copies characters from the given string into the destination character array.Syntax: public void getChars(int srhStartIndex, int srhEndIndex, char[] destArray, int destStartIndex) Parameters: srhStartIndex : Index of the first character in the string to copy. srhEnd 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 StringBuffer deleteCharAt() Method in Java with Examples The Java.lang.StringBuffer.deleteCharAt() is a built-in Java method which removes the char at the specified position in this sequence. So that the sequence is reduced by 1 char. Syntax: public StringBuffer deleteCharAt(int indexpoint) Parameters : The method accepts a single parameter indexpoint of 2 min read StringBuffer substring() method in Java with Examples In StringBuffer class, there are two types of substring method depending upon the parameters passed to it. substring(int start) The substring(int start) method of StringBuffer class is the inbuilt method used to return a substring start from index start and extends to end of this sequence. The strin 4 min read StringBuffer codePointAt() method in Java with Examples The codePointAt() method of StringBuffer class returns a character Unicode point at that index in sequence contained by StringBuffer. This method returns the âUnicodenumberâ of the character at that index. Value of index must be lie between 0 to length-1. If the char value present at the given index 2 min read Like