Java String.copyValueOf() Method Last Updated : 24 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 ArrayCopy Subarray1. Copy Entire Character ArrayThis 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 // 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); } } OutputThe new copied string is: Sweta Dash 2. Copy SubarrayThis 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 copycount: Number of characters to copyReturn Value: It returns a string consisting of the specified subarray of the character array.Example: Java // 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); } } OutputThe new copied string is: Sweta Application Example: Extracting SubstringsThis 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 // 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); } } OutputThe original array is: Rs 1024 The new string is: 1024 Comment More infoAdvertise with us Next Article Java String.copyValueOf() Method A Astha Tyagi Improve Article Tags : Strings Java DSA Java-Strings Java-lang package +1 More Practice Tags : JavaJava-StringsStrings Similar Reads Java Vector copyInto() Method In Java, the copyInto() method is used to copy all the components from one vector to another array, having enough space to hold all of the components of the vector. It is to be noted that the index of the elements remains unchanged. The elements present in the array are replaced by the elements of t 3 min read CopyOnWriteArrayList toString() method in Java The toString() method of CopyOnWriteArrayList returns the string representation of the list. Syntax: public String toString() Parameters: The function does not accepts any parameter. Return Value: The function returns the string representation of the list. Below programs illustrate the above functio 1 min read Java Vector clone() Method In Java, the clone() method of the Vector class is used to return a shallow copy of the vector. It just creates a copy of the vector. The copy will have a reference to a clone of the internal data array but not a reference to the original internal data array.Example 1: In this example, we use the cl 3 min read Java String valueOf() Method The valueOf() method of the String class in Java helps to convert various data types like integers, floats, booleans, and objects into their string representations. It makes it simple to work with string manipulation, logging, and displaying data efficiently.Example:To convert a number into text for 3 min read Files copy() Method in Java with Examples The copy() method of java.nio.file.Files Class is used to copy bytes from a file to I/O streams or from I/O streams to a file. I/O Stream means an input source or output destination representing different types of sources e.g. disk files. Methods: Based on the type of arguments passed, the Files cla 5 min read Stack copyInto() method in Java with Example The java.util.Stack.copyInto() method is used to copy all of the components from this Stack to another Stack, having enough space to hold all of the components of the Stack. It is to be noted that the index of the elements remains unchanged. The elements present in the Stack are replaced by the elem 2 min read Object toString() Method in Java Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class, henceforth, it is a child of the Object class. If a class does not extend any other class then it is a direct child class of Object, and if it extends another class, then it is 3 min read TreeSet clone() Method in Java The Java.util.TreeSet.clone() method is used to return a shallow copy of the mentioned tree set. It just creates a copy of the set. Syntax: Tree_Set.clone() Parameters: The method does not take any parameters. Return Value: The function just returns a copy of the TreeSet. Below program illustrates t 1 min read EnumSet copyOf() Method in Java The java.util.EnumSet.copyOf(Collection collect) method in Java is used to copy all of the contents from a collection to a new enum set. At first, the collection is made out of the elements of the enum and then a new enum set is created, which is the copy of the collection. Syntax: New_Enum_Set = En 3 min read Stack clone() method in Java with Example The clone() method of Stack class is used to return a shallow copy of this Stack. It just creates a copy of the Stack. The copy will have a reference to a clone of the internal data array but not a reference to the original internal data array. Syntax: Stack.clone()Parameters: The method does not ta 2 min read Like