Java: String copyValueOf(char[] data) method

In this article, we will discuss how to convert character array (char[]) to string using String’s copyValueOf() method

 

copyValueOf(char[] data) method: This String method converts or represents the character array passed (as input parameter) to String equivalent

Note: This String method replaces existing string content/value (when invoking with existing string reference)

 

Method Signature:

public static String copyValueOf(char[] data);

 

Returns:

Returns a String representation of the character array

 

Examples on copyValueOf(char[] data) method:

Below demo program converts character array into String equivalent

StringCopyValueOfMethod.java

package in.bench.resources.string.methods;

public class StringCopyValueOfMethod {

	public static void main(String[] args) {

		char[] charWebValue = {'B', 'e', 'n', 'c','h',
                  'R', 'e', 's', 'o', 'u', 'r', 'c', 'e', 's'};
		String strTest1 = "HelloWorld";

		// assigning to new string (accessing static way)
		String newStr = String.copyValueOf(charWebValue);

		// printing to console
		System.out.println("Assigning to NEW string : " + newStr);

		// Note: invoking & assigning to existing string
		strTest1 = strTest1.copyValueOf(charWebValue);

		// printing to console
		System.out.println("\nAssigning to EXISTING string : "+ strTest1);
	}
}

 

Output:

Assigning to NEW string : BenchResources

Assigning to EXISTING string : BenchResources

 

 

Variation:

There is one more variation to this String method i.e.; passing initial offset and count values in addition to character array

 

Method Signature:

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

 

Returns:

Returns a String representation of the character array

 

Examples on copyValueOf(char[] data, int offset, int count) method:

Below demo program converts character array into String equivalent

package in.bench.resources.string.methods;

public class StringCopyValueOfMethod {

	public static void main(String[] args) {

		char[] charWebValue = {'B', 'e', 'n', 'c','h', 
                  'R', 'e', 's', 'o', 'u', 'r', 'c', 'e', 's'};
		String strTest1 = "HelloWorld";

		// assigning to new string (accessing static way)
		String newStr = String.copyValueOf(charWebValue, 5, 8);

		// printing to console
		System.out.println("Assigning to NEW string : " + newStr);

		// Note: invoking & assigning to existing string
		strTest1 = strTest1.copyValueOf(charWebValue, 5, 8);

		// printing to console
		System.out.println("\nAssigning to EXISTING string : "+ strTest1);
	}
}

 

Output:

Assigning to NEW string : Resource

Assigning to EXISTING string : Resource

 

 

References:

https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html

 

Read Also:

  • Java: String class
  • Java: String Literal and String constant pool concept
  • Java: String is Immutable
  • Java: String comparison
  • Java: String concatenation

 

Happy Coding !!
Happy Learning !!