Java String copyValueOf() Method

You are Here:

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); } }

Output

Returned String: wikimass

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.

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on Java with examples for quick and easy learning.

We are working to cover every Single Concept in Java.

Please do google search for:

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