The Wayback Machine - https://web.archive.org/web/20230323162012/https://www.geeksforgeeks.org/java-lang-string-copyvalueof-java/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Java.lang.String.copyValueOf() in Java

Improve Article
Save Article
  • Last Updated : 28 Aug, 2017
Improve Article
Save Article

The copyValueOf() primarily copies the content of a character array into the string. Two variants of this function are available and both are discussed in this article.
Implementation 1 :

Syntax:
public static String copyValueOf(char[] ch)
Parameters:
ch : The character array.
Return Value : 
This function returns the string with the contents of character array copied 




// Java code to demonstrate the working of 
// copyValueOf implementation 1 
public class Copy1 {
  public static void main(String args[]) {
        
    // Initialising Character array
     char[] ch = {'A', 's', 't', 'h', 'a', ' ', 'T', 'y', 'a', 'g', 'i'};
       
     // Initialising String
     String ch2 = "";
       
     // Copying value in ch2
     // now ch2 is equal to "Astha Tyagi"
     ch2 = ch2.copyValueOf( ch );
       
     // Printing String
     System.out.println("The new copied string is : " + ch2);
  }
}

Output:

The new copied string is : Astha Tyagi

In the second implementation, the subarray can also be extracted instead of whole array.
Implementation 2 :

Syntax:
public static String copyValueOf(char[] ch, int index, int num)
Parameters:
ch : The character array.
index :  The starting position of array from which copy is to start.
num :  Number of elements that has to be copied. 
Return Value : 
This function returns the string with the contents of character array copied




// Java code to demonstrate the working of 
// copyValueOf implementation 2
public class Copy2 {
  public static void main(String args[]) {
        
    // Initialising Character array
     char[] ch = {'A', 's', 't', 'h', 'a', ' ', 'T', 'y', 'a', 'g', 'i'};
       
     // Initialising String
     String ch2 = "";
       
     // Copying value in ch2
     // only first 5 are extracted
     // now ch2 is equal to "Astha"
     ch2 = ch2.copyValueOf( ch , 0, 5 );
       
     // Printing String
     System.out.println("The new copied string is : " + ch2);
  }
}

Output :

The new copied string is : Astha

Possible application : 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 1000” type of strings which deal with denominations.




// Java code to demonstrate the application of 
// copyValueOf 
public class Appli2 {
  public static void main(String args[]) {
        
    // Initialising Character array
     char[] ch = {'R', 's', ' ', '1', '0', '2', '4' };
       
     // Original array
     System.out.print("The original array is : ");
     for (int i=0; i< ch.length ; i++) 
     System.out.print(ch[i]);
       
      System.out.print("\n");
       
     // Initialising String
     String ch2 = "";
       
     // Copying value in ch2
     // Rs is not included
     // now ch2 is equal to "1024"
     ch2 = ch2.copyValueOf( ch , 3, 4 );
       
     // Printing String
     System.out.println("The new string is : " + ch2);
  }
}

Output :

The original array is : Rs 1024
The new string is : 1024

This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!