Open In App

Java String.copyValueOf() Method

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
4 Likes
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 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:


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:


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:


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

Next Article
Article Tags :
Practice Tags :

Similar Reads