java.lang.StringBuilder getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

Description

On this document we will be showing a java example on how to use the getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method of StringBuilder Class. Basically the getChars() method copied 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

Notes:

The method getChars() will throw IndexOutOfBoundsException if any of the following condition is true

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

Method Syntax

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

Method Argument

Data Type Parameter Description
int srcBegin start copying at this offset.
int srcEnd stop copying at this offset.
char[] dst the array to copy the data into.
int dstBegin offset into dst.

Method Returns

The getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method returns void.

Compatibility

Requires Java 1.5 and up

Java StringBuilder getChars() Example

Below is a java code demonstrates the use of getChars() method of StringBuilder class. The example presented might be simple however it shows the behavior of the getChars() method.

The java example source code above demonstrates the use of getChars() method of StringBuilder class. Initially the code assigns a string “javatutorialhq.com” as initial contents of the StringBuilder. Then we use getChars() method to get characters from the buffer and put it on the the character array as method argument.

Sample Output

Below is the sample output when you run the above example.

StringBuilder getchars() example output