Java.lang.StringBuilder.getChars() Method



The java.lang.StringBuilder.getChars() method copies the characters from this sequence into the destination character array dst.

The first character to be copied is at index srcBegin; the last character to be copied is at index srcEnd - 1. The total number of characters to be copied is srcEnd - srcBegin. The characters are copied into the subarray of dst starting at index dstBegin and ending at index: dstbegin + (srcEnd-srcBegin) - 1


Following is the declaration for java.lang.StringBuilder.getChars() method

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


  • srcBegin − This means start copying at this offset.

  • srcEnd − This means stop copying at this offset.

  • dst − This is the array to copy the data into..

  • dstBegin − This is the offset into dst.

Return Value

This method does not return any value.


  • NullPointerException − if dst is null.

  • IndexOutOfBoundsException − this is thrown if any of the following is true −

srcBegin is negative
dstBegin is negative
the srcBegin argument is greater than the srcEnd argument.
srcEnd is greater than this.length().
dstBegin + srcEnd - srcBegin is greater than dst.length 


The following example shows the usage of java.lang.StringBuilder.getChars() method.

package com.tutorialspoint;

import java.lang.*;

public class StringBuilderDemo {

   public static void main(String[] args) {
      StringBuilder str = new StringBuilder("java programming");
      System.out.println("string = " + str);

      // char array
      char[] cArr = new char[]{'t','u','t','o','r','i','a','l','s'};

      // copy the chars from index 5 to index 10 into subarray of cArr
      // the offset into destination subarray is set to 3
      str.getChars(5, 10, cArr, 3);
      // print character array

Let us compile and run the above program, this will produce the following result −

string = java programming