The java string getChars() method copies characters from the given string into the destination character array.
Syntax:
public void getChars(int srhStartIndex,
int srhEndIndex, char[] destArray, int destStartIndex)
Parameters:
srhStartIndex : Index of the first character in the string to copy.
srhEndIndex : Index after the last character in the string to copy.
destArray : Destination array where chars wil get copied.
destStartIndex : Index in the array starting from where the chars
will be pushed into the array.
Return: It does not return any value.
Exception: StringIndexOutOfBoundsException – If srhStartIndex, srhEndIndex are not in proper range.
Example : To show working of getChars() method
// Java program to demonstrate // working of getChars() method class Gfg1 { public static void main(String args[]) { String str = "Welcome! to GeeksforGeeks"; char[] destArray = new char[20]; try { str.getChars(12, 25, destArray, 0); System.out.println(destArray); } catch (Exception ex) { System.out.println(ex); } } } |
chevron_right
filter_none
Output:
GeeksforGeeks
// Java program to demonstrate // exception condition in // working of getChars() method class Gfg2 { public static void main(String args[]) { String str = "Welcome! to GeeksforGeeks"; char[] destArray = new char[20]; try { // Starting index 0 and ending index 24 str.getChars(12, 26, destArray, 0); System.out.println(destArray); } catch (Exception ex) { System.out.println(ex); } } } |
chevron_right
filter_none
Output:
java.lang.StringIndexOutOfBoundsException: String index out of range: 26
Recommended Posts:
- StringBuilder getChars() in Java with Examples
- StringBuffer getChars() method in Java with Examples
- Java String format() with examples
- Java String concat() with examples
- Java String endsWith() with examples
- Java String join() with examples
- Java String toLowerCase() with examples
- Java String subSequence() method with Examples
- String toString() Method in java with Examples
- ZoneOffset of(String) method in Java with Examples
- Split() String method in Java with examples
- Matcher start(String) method in Java with Examples
- Matcher group(String) method in Java with Examples
- Matcher replaceAll(String) method in Java with Examples
- Matcher quoteReplacement(String) method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



