In Java, getChars(srcBegin, srcEnd, dst, dstBegin) copies the characters from srcBegin up to (not including) srcEnd into the array dst, starting at index dstBegin.
Unlike toCharArray(), it does not create an array: you supply one, and you can fill it at any position. That suits buffers, fixed-width records, and code that copies text piece by piece.
getChars() is an instance method of java.lang.String, available since JDK 1.0. Call it on a string, as in text.getChars(...); calling it on a null reference throws NullPointerException. It only reads the text; the string itself is never changed.
Syntax
Give the source range, the target array, and where in the target the copy should begin.
java
char[] buffer = new char[5];
text.getChars(6, 11, buffer, 0); // copy text[6..10] into buffer[0..4]
Call
What it does
"Hello".getChars(0, 2, buf, 0)
Copies 'H', 'e' to buf[0], buf[1].
"Hello".getChars(1, 4, buf, 2)
Copies "ell" into buf[2..4].
"Hello".getChars(2, 2, buf, 0)
Copies nothing (empty range).
The number of chars copied is srcEnd - srcBegin. The target must have room for all of them from dstBegin on; existing elements outside that range keep their values.
Library declarations
java
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);
Parameters
Parameter
Description
srcBegin
Index of the first character to copy.
srcEnd
Index after the last character to copy (exclusive).
dst
The destination array. Must not be null.
dstBegin
Where in dst the first copied char goes.
Return Value
Return Value
Description
void
Nothing. The result is written into dst.
Java Examples
Example 1 — Copy a Word into a New Buffer
Pull "World" out of "Hello World" into a five-slot array.
java
import java.util.Arrays;
public class GetCharsBasic {
public static void main(String[] args) {
String text = "Hello World";
char[] buffer = new char[5];
text.getChars(6, 11, buffer, 0);
System.out.println(Arrays.toString(buffer));
System.out.println(new String(buffer));
}
}
Output
[W, o, r, l, d]
World
How It Works
Prepare the target.new char[5] has exactly enough room for the five letters to copy.
Source range 6 to 11. Index 6 is 'W' and 11 is the length, so the copy covers W o r l d; nothing is returned.
Read the result from the array.Arrays.toString lists the slots and new String(buffer) prints them as one word.
Example 2 — Fill the Middle of an Existing Array
Overwrite parts of a template line while the rest of the array stays as it was.
java
public class GetCharsIntoMiddle {
public static void main(String[] args) {
char[] line = "[.....]".toCharArray();
"abc".getChars(0, 3, line, 2);
System.out.println(new String(line));
"xyz".getChars(1, 3, line, 1);
System.out.println(new String(line));
}
}
Output
[.abc.]
[yzbc.]
How It Works
A 7-slot template. The array starts as [, five dots, and ].
dstBegin picks the spot. Copying "abc" to position 2 replaces slots 2-4, leaving the dots at 1 and 5 in place.
Partial source ranges."xyz".getChars(1, 3, line, 1) copies only "yz" into slots 1-2, overwriting the dot and the 'a'.
Example 3 — What Happens When the Buffer Is Too Small
Try to copy five characters into a three-slot array, then copy a range that fits.
java
public class GetCharsTooSmall {
public static void main(String[] args) {
String word = "Hello";
char[] small = new char[3];
try {
word.getChars(0, 5, small, 0);
} catch (IndexOutOfBoundsException e) {
System.out.println("5 chars do not fit into 3 slots");
}
word.getChars(0, 3, small, 0);
System.out.println(new String(small));
System.out.println(word);
}
}
Output
5 chars do not fit into 3 slots
Hel
Hello
How It Works
Bounds are checked first. Five chars starting at slot 0 need slots 0-4, but the array ends at 2, so getChars throws an IndexOutOfBoundsException.
Copy what fits. The range 0 to 3 copies "Hel" into the three slots.
The string is only read.word still prints "Hello"; getChars wrote only to the array.
Frequently Asked Questions
It copies a range of characters from the string into a char array you provide, starting at a chosen index in that array.
Nothing. It is a void method; the copied characters are written into the dst array.
toCharArray() creates a new array with all characters. getChars() copies only a range into an existing array at any position.
No. srcEnd is exclusive, so srcEnd - srcBegin characters are copied.
IndexOutOfBoundsException (StringIndexOutOfBoundsException in OpenJDK) for invalid indexes or a destination that is too small, and NullPointerException for a null array.