Java String getChars()24 Mar 2025 | 3 min read The Java String class getChars() method copies the content of this string into a specified char array. There are four arguments passed in the getChars() method. The signature of the getChars() method is given below: SignatureParametersint srcBeginIndex: The index from where copying of characters is started. int srcEndIndex: The index which is next to the last character that is getting copied. Char[] destination: The char array where characters from the string that invokes the getChars() method is getting copied. int dstEndIndex: It shows the position in the destination array from where the characters from the string will be pushed. ReturnsIt doesn't return any value. Exception ThrowsThe method throws StringIndexOutOfBoundsException when any one or more than one of the following conditions holds true.
Internal implementationThe signature or syntax of string getChars() method is given below: Java String getChars() Method ExampleExampleCompile and RunOutput: TpointTech Java String getChars() Method Example 2The method throws an exception if index value exceeds array range. Let's see an example. ExampleCompile and RunOutput: java.lang.StringIndexOutOfBoundsException: offset 10, count 14, length 20 Java String getChars() Method Example 3The getChars() method does not copy anything into the char array, provided the value of srcBeginIndex and srcEndIndex are the same. It is because the getChars() method copies from the srcBeginIndex index to srcEndIndex - 1 index. As srcBeginIndex is equal to srcEndIndex; therefore, srcEndIndex - 1 is less than srcBeginIndex. Therefore, the getChars() method copies nothing. The following example confirms the same. ExampleCompile and RunOutput: The getChars() method prints nothing as start and end indices are equal. Next TopicJava String indexOf() |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India