string.getChars()

String.getChars() Creates a byte array by using sequence of bytes used in string.
Syntax
String1.getChars(srcStart,srcEnd,dest_array,dest_start)
srcStart : Integer , starting position of input string from where the copying will start.
srcEnd : Integer , Ending position of input string from where the copying will end.
dest_array : Array, to where the copied chars will be stored.
dest_start : Integer, Postion of the dest_array from where copied chars will be stored.

Return the dest_array with all copied chars

Here are some examples with output.
String my_str="Welcome to plus2net";
char[] my_array=new char[5];
my_str.getChars(2,5,my_array,2);
System.out.println(my_array);//  lco
In above code the 3 chars from 2nd position( srcStart) to 4th position ( srcEnd) is copied and stored at postion 2 (dest_start) of the array my_array( dest_array).

We have declared the array having 5 positions, what happens if the number of chars copied are more than the positions available in destination array ?

In the below code we have given postion 4 for the destination array.
String my_str="Welcome to plus2net";
char[] my_array=new char[5];
my_str.getChars(2,5,my_array,4);//  lco
System.out.println(my_array);
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: offset 4, count 3, length 5

Above error is generated as we can't store 3 chars starting from 4th position in an array of length = 5.

All String functions


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer