Java String copyValueOf() Method

Java String copyValueOf()

The copyValueOf() method copies an array of characters to the string.

Note: This method does not append the content in String, instead it replaces the existing string value with the sequence of characters of array.

Example

Java Compiler
public class myClass { public static void main(String[] args) { char[] myStr1 = {'w', 'i', 'k', 'i', 'm', 'a', 's', 's'}; String myStr2 = ""; myStr2 = myStr2.copyValueOf(myStr1, 0, 8); System.out.print("Returned String: " + myStr2); } }

Syntax

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

Parameter Values

ValueTypeExplanation
dataRequiredSpecifies the char array.
offsetRequiredSpecifies the start index of the char array.
countRequiredSpecifies the length of the char array.

Return Value

ValueExplanation
NumberReturns the characters of the char array.
StringIndexOutOfBoundsExceptionThis error occurs when any of the following condition satisfied.
  • If offset is negative.
  • If offset is out of reach.
  • If count is greater than the length of the char array.
  • If count is negative.

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author