STRING INSTANCE METHODS
Java String getChars() method
getChars() copies a selected part of a String string into an existing char[] array. It does not change the string or return a new array: it writes characters into the supplied destination.
Copying part of a string to a char array
import java.util.Arrays;
String tekstas = "JavaQuest";
char[] raides = new char[4];
tekstas.getChars(0, 4, raides, 0);
System.out.println(Arrays.toString(raides));
System.out.println(raides);[J, a, v, a]
Java
tekstasstores the value JavaQuest.raidesis a four-elementchar[]array.getChars(0, 4, raides, 0)takes characters from index0to index4, excluding index4itself.- Characters are written into the
raidesarray starting at destination index0. Arrays.toString()displays array elements separately, whileSystem.out.println(raides)prints the characters as text.
Why is getChars() an instance method?
getChars() is called on a specific string because it copies that String object's characters:
tekstas.getChars(0, 4, raides, 0);
On the left, tekstas is the character source. The arguments specify the source bounds, destination array and where writing begins in that array.
Method syntax
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
| Parameter | Meaning |
|---|---|
srcBegin | Index of the first character to copy from the string. |
srcEnd | Position after the last character to copy; this bound is exclusive. |
dst | Destination char[] array into which characters are written. |
dstBegin | Index in the destination array where writing starts. |
| Return type | void, so the method returns nothing. |
| Errors | Invalid source bounds or an undersized destination array cause index errors. |
Copying from the middle of a string
String tekstas = "Mokausi Java";
char[] dalis = new char[4];
tekstas.getChars(8, 12, dalis, 0);
System.out.println(dalis);Java
getChars(8, 12, dalis, 0) copies four characters from the middle of the sentence. The starting index, 8 , is inclusive, and the ending index, 12 , is exclusive, producing the text Java.
Writing after the start of the array
import java.util.Arrays;
String tekstas = "Java";
char[] rezultatas = {'-', '-', '-', '-', '-', '-'};
tekstas.getChars(0, 4, rezultatas, 1);
System.out.println(Arrays.toString(rezultatas));
System.out.println(rezultatas);[-, J, a, v, a, -]
-Java-
dstBegin selects where writing starts in the destination array. In this example, the text Java is written from index 1, leaving the first and last array characters unchanged.
srcEnd is not the last character's index
String tekstas = "JavaQuest";
char[] raides = new char[5];
tekstas.getChars(4, 9, raides, 0);
System.out.println(raides);Quest
In the text JavaQuest , the letter Q is at index 4, while the final letter t is at index 8. To include t, use srcEnd as a 9because the end boundary is exclusive.
getChars() returns nothing
String tekstas = "Java";
char[] raides = new char[4];
tekstas.getChars(0, 4, raides, 0);
System.out.println(raides);Java
The return type is void, so you cannot write char[] raides = tekstas.getChars(...). First create the destination array, then let the method fill its elements.
The difference from charAt()
String tekstas = "Java";
char vienaRaide = tekstas.charAt(0);
char[] keliosRaides = new char[2];
tekstas.getChars(0, 2, keliosRaides, 0);
System.out.println(vienaRaide);
System.out.println(keliosRaides);J
Ja
charAt() retrieves one character by index. getChars() is more convenient for copying several characters at once into an existing char[] array.
The difference from toCharArray()
String tekstas = "Java";
char[] visos = tekstas.toCharArray();
char[] dalis = new char[2];
tekstas.getChars(1, 3, dalis, 0);
System.out.println(visos);
System.out.println(dalis);Java
av
toCharArray() creates a new array from the whole string. getChars() copies only a selected range into an array you have already prepared.
Source bounds cannot exceed the string
String tekstas = "Java";
char[] raides = new char[4];
tekstas.getChars(0, 5, raides, 0);StringIndexOutOfBoundsException
The text's Java has length 4, so the largest valid end boundary is 4. When passed 5, Java cannot copy a nonexistent character range.
The destination array needs enough space
String tekstas = "Java";
char[] raides = new char[2];
tekstas.getChars(0, 4, raides, 0);StringIndexOutOfBoundsException
The example tries to copy four characters into an array with only two slots. getChars() cannot fit all the characters into the destination array, so an error occurs.
Negative indices are invalid
String tekstas = "Java";
char[] raides = new char[4];
tekstas.getChars(-1, 2, raides, 0);StringIndexOutOfBoundsException
As with most Java String and array operations, indices start at 0. A negative start is invalid, making this call incorrect.
Checking bounds safely
String tekstas = "JavaQuest";
int pradzia = 0;
int pabaiga = 4;
char[] raides = new char[pabaiga - pradzia];
if (pradzia >= 0 && pabaiga <= tekstas.length() && pradzia <= pabaiga) {
tekstas.getChars(pradzia, pabaiga, raides, 0);
System.out.println(raides);
} else {
System.out.println("Netinkamos ribos");
}Java
If indices come from users or another program component, validate them before copying. A convenient destination-array size is pabaiga - pradzia, exactly the number of characters being copied.
Common mistakes
- Expecting
getChars()to returnchar[]although the method isvoid. srcEndis treated as the last character's index, although it is the boundary after the last copied character.- Creating a destination array that is too small.
- Forgetting that
dstBeginalso reduces the remaining space in the destination array. - Using negative indices or ones beyond the string's length.
- Unnecessarily using
getChars()for the entire string instead of the simplertoCharArray().
When should you use getChars()?
getChars() Use it to copy a specific string range into an existing char[] array, for example with older APIs, buffers or manual character processing. For one character, choose charAt(). For the whole string as a new array, use toCharArray().