w3resource logo


Introduction to Java Programming

Java String: copyValueOf() Method

Secondary Nav

copyValueOf() Method

Contents:

public static String copyValueOf(char[] data)

Equivalent to valueOf(char[]).

Java Platform: Java SE 8

Syntax :

copyValueOf(char[] data)

Parameters:

Name Description Type
data the character array. String

Return Value : a String that contains the characters of the character array.

Return Value Type: String

Example : Java String copyValueOf() Method

The following example shows the usage of java String() method.

public class Example {

public static void main(String[] args)
    {
        // Character array with data.
char[] arr_num = new char[] { '1', '2', '3', '4' };

        // Create a String containig the contents of arr_num
        // starting at index 1 for length 2.
        String str = String.copyValueOf(arr_num, 1, 3);

        // Display the results of the new String.
System.out.println("\nThe book contains " + str +" pages.\n");
    }
}

Output:

The book contains 234 pages.

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

Equivalent to valueOf(char[], int, int).

Java Platform: Java SE 8

Syntax :

copyValueOf(char[] data, int offset, int count)

Parameters:

Name Description Type
data the character array. String
offset initial offset of the subarray. String
count the length of the subarray. String

Return Value : a String that contains the characters of the specified subarray of the character array.

Return Value Type: String

Throws:

IndexOutOfBoundsException - if offset is negative, or count is negative, or offset+count is larger than data.length.

Example : Java String copyValueOf() Method

The following example shows the usage of java String() method.

public class Example {

  public static void main(String[] arg) {
    char[] textArray = { 'w', '3', 'r', 'e', 's', 'o', 'u', 'c', 'o', 'm', ' ', 'j', 'a', 'v', 'a',
        'r', 'c','e', '.', 'c', 'o', 'm' };

    String text = String.copyValueOf(textArray, 6, 2);
    System.out.println();
    System.out.println(text);
    System.out.println();
  }

}

Output:

uc 

Java Code Editor:







Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Java: How to convert a string to an integer in Java?

C#: Loops in c#

SQL: JOIN using more than 5 tables