Java String copyValueOf() method
copyValueOf() method returns a String that holds the character sequence of the character array.
Syntax:
copyValueOf(char[] data)
Parameters:
data : the character array i.e. String
Returns:
It returns a String that contains the characters of the character array.
Java String copyValueOf() Example 1:
public class JavaCopyValueOfEx1 {
public static void main(String args[]) {
char[] data = {'a','b','c','d','e','f','g','h','i','j','k'};
String str1 = "Text";
str1 = str1.copyValueOf(data);
System.out.println("str1 after copy: " + str1);
}
}
Output:
str1 after copy: abcdefghijk
Java String copyValueOf() Example 2:
public class JavaCopyValueOfEx2 {
public static void main(String args[]) {
char[] data = {'a','b','c','d','e','f','g','h','i','j','k'};
String str1 = "Text";
String str2 = "String";
//Variation 1:String copyValueOf(char[] data)
str1 = str1.copyValueOf(data);
System.out.println("str1 after copy: " + str1);
//Variation 2:String copyValueOf(char[] data,int offset,int count)
str2 = str2.copyValueOf(data, 5, 3 );
System.out.println("str2 after copy: " + str2);
}
}
Output:
str1 after copy: abcdefghijkstr2 after copy: fgh
Java String copyValueOf() Example 3:
public class JavaCopyValueOfEx3
{
public static void main(String[] args)
{
// Character array with data.
char[] arr_num = new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
// Create a String containig the contents of arr_num
// starting at index 1 for length 2.
String str = String.copyValueOf(arr_num, 4, 3);
// Display the results of the new String.
System.out.println("\nThis Java Book contains " + str +" pages.\n");
}
}
Output:
This Java Book contains 567 pages.
Java String copyValueOf() Example 4:
public class JavaCopyValueOfEx4 {
public static void main(String args[]) {
// Initialising Character array
char[] ch = {'R', 'O', 'Y', ' ', 'N', 'I', 'T', 'I', 'S', 'H'};
// Initialising String
String ch2 = "";
// Copying value in ch2
// now ch2 is equal to "ROY NITISH"
ch2 = ch2.copyValueOf( ch );
// Printing String
System.out.println("copied string is : " + ch2);
}
}
Output:
copied string is : ROY NITISH
