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[] 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 dest array to start pasting the copied data.

Returns: This method returns nothing.

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 StringBuffer Class:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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] + " ");
        }
    }
}

chevron_right


Output:

String = Geeks For Geeks
char array contains : . G e e k s   F o r . . . . .

Example 2: To demonstrate StringIndexOutOfBoundsException.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet