Open In App

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.

Syntax:



public void getChars(int srcBegin, int srcEnd, 
                          char[] dst, int dstBegin)

Parameters: This method accepts four different parameters:

Return Value: This method does not return anything.



Exception: This method throws StringIndexOutOfBoundsException if:

Below programs demonstrate the getChars() method of StringBuilder Class:

Example 1:




// 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 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 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)


Article Tags :