StringBuilder getChars() in Java with Examples Last Updated : 22 May, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 StringBuilder into the array dst[] starting at index:dstBegin and ending at index:dstbegin + (srcEnd-srcBegin) - 1. The first character to be copied from StringBuilder 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 accepts four different parameters: srcBegin: represents index on which we have to start copying. srcEnd: represents index on which we have to stop copying. dst: represents the array to copy the data into. dstBegin: represents index of dest array we start pasting the copied data. Return Value: This method does not return anything. Exception: This method throws StringIndexOutOfBoundsException if: srcBegin < 0 dstBegin < 0 srcBegin > srcEnd srcEnd > this.length() dstBegin+srcEnd-srcBegin > dst.length Below programs demonstrate the getChars() method of StringBuilder Class: Example 1: Java // Java program to demonstrate // the getChars() 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()); // create a char Array char[] array = new char[7]; // get char from index 0 to 7 // and store in array start index 0 str.getChars(0, 7, array, 0); // 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 = WelcomeGeeks Char array contains : W e l c o m e Example 2: Java // Java program to demonstrate // the getChars() Method. import java.util.*; class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("evil dead_01"); // print string System.out.println("String = " + str.toString()); // create a char Array char[] array = new char[10]; // initialize all character to _(underscore). Arrays.fill(array, '_'); // get char from index 5 to 9 // and store in array start index 3 str.getChars(5, 9, array, 3); // 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 = evil dead_01 char array contains : _ _ _ d e a d _ _ _ Example 3: To demonstrate StringIndexOutOfBoundException Java // Java program to demonstrate // exception thrown by the getChars() Method. import java.util.*; class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("evil dead_01"); // create a char Array char[] array = new char[10]; // initialize all character to _(underscore). Arrays.fill(array, '_'); try { // if start is greater then end str.getChars(5, 2, array, 0); } catch (Exception e) { System.out.println("Exception: " + e); } } } Output: Exception: java.lang.StringIndexOutOfBoundsException: srcBegin > srcEnd Reference: https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.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-Strings Java-lang package Java-Functions Java-StringBuilder +2 More Practice Tags : JavaJava-Strings Similar Reads 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 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 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 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 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 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 Like