Java String - copyValueOf() Method
The Java copyValueOf() method returns a string that contains the characters of specified array or specified subarray of the character array.
Syntax
public static String copyValueOf(char[] data) public static String copyValueOf(char[] data, int offset, int count)
Parameters
data |
specify the array of characters. |
offset |
specify the initial offset of the subarray. |
count |
specify the length of the subarray. |
Return Value
Returns a string that contains the characters of specified array or specified subarray of the character array.
Exception
Throws IndexOutOfBoundsException, if offset is negative, or count is negative, or offset+count is larger than data.length.
Example:
In the example below, copyValueOf() method returns a string that contains the characters of specified array.
public class MyClass { public static void main(String[] args) { char[] data = {'H', 'E', 'L', 'L', 'O'}; String str = String.copyValueOf(data); System.out.println(str); } }
The output of the above code will be:
HELLO
Example:
In the example below, the method returns a string that contains the specified subarray of the character array.
public class MyClass { public static void main(String[] args) { char[] data = {'B', 'E', 'H', 'O', 'L', 'D'}; String str = String.copyValueOf(data, 2, 4); System.out.println(str); } }
The output of the above code will be:
HOLD
❮ Java String Methods