Java String copyValueOf() Method Tutorial

In this section, we will learn what the String copyValueOf() method is and how to use it in Java.

What is Java String copyValueOf() Method?

The Java String copyValueOf() method is used to copy the content of a char array and create a new string object as a result.

Java copyValueOf() Method Syntax:

public static String copyValueOf(char[] data, int offset, int count)

copyValueOf() Method Parameters:

  • `data`: the argument we set here is the target `char array` that we want to copy its content.
  • `offset`: this is the index number of the `char array` that we want to start from there and copy the elements.
  • `count`: the argument we set here is the number of elements we want to be copied from the target `char array` starting from the offset.

copyValueOf() Method Return Value:

The return value of this method is a new string object which contains the elements of the target char array that we set as the argument of this method.

Example: using String copyValueOf() method

public class Simple {

    public static void main(String[] args) {
        char[] chars = {'h','e','l','l','o',' ','w','o','r','l','d'};
        String copy = "";
        copy = copy.copyValueOf(chars, 6, 5);

Output: world


Top Technologies