Java String.copyValueOf() Method
The copyValueOf() method in Java is used to create a new String by copying characters from a character array. It is a static method of the String class.
Variants: There are two variants of this method.
- Copy Entire Character Array
- Copy Subarray
1. Copy Entire Character Array
This method copies the entire contents of a character array into a new string.
Syntax:
public static String copyValueOf(char[] ch)
- Parameter: ch: The character array to copy.
- Return Value: It returns a string containing the contents of the character array.
Example:
// Java code to demonstrate the working of
// copyValueOf implementation 1
public class Geeks {
public static void main(String args[]) {
// Initialising Character array
char[] ch = {'S', 'w', 'e', 't', 'a', ' ', 'D', 'a', 's', 'h'};
// Copying value into a String
String ch2 = String.copyValueOf(ch);
System.out.println("The new copied string is: " + ch2);
}
}
// Java code to demonstrate the working of
// copyValueOf implementation 1
public class Geeks {
public static void main(String args[]) {
// Initialising Character array
char[] ch = {'S', 'w', 'e', 't', 'a', ' ', 'D', 'a', 's', 'h'};
// Copying value into a String
String ch2 = String.copyValueOf(ch);
System.out.println("The new copied string is: " + ch2);
}
}
Output
The new copied string is: Sweta Dash
2. Copy Subarray
This method copies a specific portion that is a subarray of a character array into a new string.
Syntax:
public static String copyValueOf(char[] ch, int index, int count)
- Parameter:
- ch: The character array to copy.
- index: Starting index to copy
- count: Number of characters to copy
- Return Value: It returns a string consisting of the specified subarray of the character array.
Example:
// Java code to demonstrate the working of
// copyValueOf implementation 2
public class Geeks {
public static void main(String args[]) {
// Initialising Character array
char[] ch = {'S', 'w', 'e', 't', 'a', ' ', 'D', 'a', 's', 'h'};
// Copying only first 5 characters
String ch2 = String.copyValueOf(ch, 0, 5);
System.out.println("The new copied string is: " + ch2);
}
}
// Java code to demonstrate the working of
// copyValueOf implementation 2
public class Geeks {
public static void main(String args[]) {
// Initialising Character array
char[] ch = {'S', 'w', 'e', 't', 'a', ' ', 'D', 'a', 's', 'h'};
// Copying only first 5 characters
String ch2 = String.copyValueOf(ch, 0, 5);
System.out.println("The new copied string is: " + ch2);
}
}
Output
The new copied string is: Sweta
Application Example: Extracting Substrings
This function can be used to general copy or extract only prefix or suffix from the string using implementation 2. One possible example can be extracting only the amount from the given “Rs 1024” type of strings which deal with denominations.
Illustration:
// Java code to demonstrate application of copyValueOf
public class Geeks {
public static void main(String args[]) {
// Initialising Character array
char[] ch = {'R', 's', ' ', '1', '0', '2', '4'};
// Display original array
System.out.print("The original array is: ");
for (char c : ch)
System.out.print(c);
System.out.print("\n");
// Extract only "1024"
String ch2 = String.copyValueOf(ch, 3, 4);
System.out.println("The new string is: " + ch2);
}
}
// Java code to demonstrate application of copyValueOf
public class Geeks {
public static void main(String args[]) {
// Initialising Character array
char[] ch = {'R', 's', ' ', '1', '0', '2', '4'};
// Display original array
System.out.print("The original array is: ");
for (char c : ch)
System.out.print(c);
System.out.print("\n");
// Extract only "1024"
String ch2 = String.copyValueOf(ch, 3, 4);
System.out.println("The new string is: " + ch2);
}
}
Output
The original array is: Rs 1024 The new string is: 1024