Java String getChars() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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]
CallWhat 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

ParameterDescription
srcBeginIndex of the first character to copy.
srcEndIndex after the last character to copy (exclusive).
dstThe destination array. Must not be null.
dstBeginWhere in dst the first copied char goes.

Return Value

Return ValueDescription
voidNothing. 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));
    }
}

How It Works

  1. Prepare the target. new char[5] has exactly enough room for the five letters to copy.
  2. 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.
  3. 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));
    }
}

How It Works

  1. A 7-slot template. The array starts as [, five dots, and ].
  2. dstBegin picks the spot. Copying "abc" to position 2 replaces slots 2-4, leaving the dots at 1 and 5 in place.
  3. 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);
    }
}

How It Works

  1. 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.
  2. Copy what fits. The range 0 to 3 copies "Hel" into the three slots.
  3. 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.

Next: hashCode()

Get the int hash code computed from the characters.

hashCode() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful