.getChars()
Anonymous contributor
Published Jun 6, 2023
Contribute to Docs
The .getChars()
string method copies a set of characters from a string into a destination character array.
Syntax
The basic syntax of .getChars()
in Java is:
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
A brief description of the parameters is as follows:
srcBegin
: The index to start copying the new array.scrEnd
: The index to stop copying the new array.dst
: The name of the new array.dstBegin
: The index of the destination array.
Example
In the following example, .getChars()
converts an array into a new shorter array. It then prints the new array in the console:
public class Test {public static void main(String args[]) {String OldLine = new String("Welcome to Texas.");char[] NewLine = new char[5];try {OldLine.getChars(11, 16, NewLine, 0);System.out.println(NewLine);} catch ( Exception ex) {System.out.println("The indexes are out of range.");}}}
This results in the following output:
Texas
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.