.copyValueOf()

Returns a string with characters copied from an array.

Syntax

string.copyValueOf(char[] data, int offset, int count)
  • data (required): An array of characters to be copied from.
  • offset (optional): The index of the first character to be copied.
  • count (optional): The number of characters to be copied.

Example 1

Copy all characters from the charactersArray array to the copiedCharacters string:

class CopyAllCharacters {
public static void main(String[] args) {
String copiedCharacters = "This string will be replaced with copied characters.";
char[] charactersArray = {'a','b','c','d','e'};
System.out.println(copiedCharacters.copyValueOf(charactersArray));
// Output: abcde
}
}

Example 2

Copy two characters from the array, starting at index 4:

class SpecificCharactersToCopy {
public static void main(String[] args) {
String copiedCharacters = "This string will be replaced with copied characters.";
char[] charactersArray = {'a','b','c','d','e','f','g','h','i'};
System.out.println(copiedCharacters.copyValueOf(charactersArray, 4, 2));
// Output: ef
}
}

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Java on Codecademy

Contributors