Java.lang Package Classes

Java String - copyValueOf() Method



The java.lang.String.copyValueOf() method returns a string that contains the characters of specified subarray of the character array.

Syntax

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

Parameters

data specify the array of characters.
offset specify the initial offset of the subarray.
count specify the length of the subarray.

Return Value

Returns a string that contains the characters of specified subarray of the character array.

Exception

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

Example:

In the example below, copyValueOf() method returns a string that contains the characters of specified subarray of the character array.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    char[] data = {'B', 'E', 'H', 'O', 'L', 'D'};

    String str = String.copyValueOf(data, 2, 4);
    System.out.println(str);   
  }
}

The output of the above code will be:

HOLD

❮ Java.lang - String