Let’s learn String getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method in java.
String getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method in java
String getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method copies characters from this string into the destination character array.
Syntax
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Parameters
srcBegin index of the first character in the stringto copy.
srcEnd index after the last character in the stringto copy.
dst the destination array.
dstBegin the start offset in the destination array.
Throws
IndexOutOfBoundsException – If any of the following is true:
• srcBegin is negative.
• srcBegin is greater than srcEnd
• srcEnd is greater than the length of thisstring
• dstBegin is negative
• dstBegin+(srcEnd-srcBegin) is larger than dst.length
Example
class StringGetCharsMethodDemo { public static void main(String[] args) { String s = new String("Hello World Java"); char[] ch = new char[7]; try { s.getChars(1, 6, ch, 0); System.out.print("Result = "); System.out.println(ch); } catch(Exception ex) { System.out.println("Exception : " + ex); } } }
Output:
Result = ello
Also read – switch statement in java