Home >>Java String Methods >Java String copyValueOf() Method

Java String copyValueOf() Method

Java String copyValueOf() Method

The copyValueOf() method in Java String is used to returns a String that represents the characters of a char array which returns a new String array and copies the characters into it.

Syntax

public static String copyValueOf(char[] data, int offset, int  count)

Parameter

data - This parameter is used to specified a char array

offset - This parameter is used to specified An int value which representing the start index of the char array

count - This parameter is used to specified An int value which representing the length of the char array

Technical Details

Returns - It is used to returns a String which representing the characters of the char array

ThrowsStringIndexOutOfBoundsException – it is used if offset is negative or out of reach, or if count is greater than the length of the char array, or negative

Here is an Example of Java String copyValueOf() Method:


public class MyClass 
{
public static void main(String[] args) 
{
char[] name1 = {'P', 'H', 'P', 'T', 'P', 'O', 'I', 'N', 'T'};
String name2 = "";
name2 = name2.copyValueOf(name1, 0, 9);
System.out.println("Returned String: " + name2);  
}
}

Output: Returned String: PHPTPOINT

No Sidebar ads