In this article, we will discuss how to convert character array (char[]) to string using String’s copyValueOf() method
1. String’s 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)
2. Example on copyValueOf() method:
There are 2 variants of copyValueOf() method viz.,
- copyValueOf(char[] data); method
- copyValueOf(char[] data, int offset, int count); method
2.1 Examples on copyValueOf(char[] data) method:
- Below demo program converts character[] array into String equivalent
Method Signature:
public static String copyValueOf(char[] data);
Returns:
- Returns a String representation of the character array
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
// Warning: The static method copyValueOf(char[])
// from the type String should be accessed in a static way
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
2.2 Examples on copyValueOf(char[] data, int offset, int count) method:
- There is one more variation to this String method i.e.; passing initial offset and count values in addition to character[] array
- Below demo program converts character[] array into String equivalent
Method Signature:
public static String copyValueOf(char[] data, int offset, int count);
Returns:
- Returns a String representation of the character array
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, 5, 8);
// printing to console
System.out.println("Assigning to NEW string : "
+ newStr);
// Note: invoking & assigning to existing string
//Warning: The static method copyValueOf(char[], int, int)
// from the type String should be accessed in a static way
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
Related Articles:
- Java – String charAt(int index) method
- Java – String compareTo(String anotherString) method
- Java – String compareToIgnoreCase(String str) method
- Java – String concat(String str) method
- Java – String contains(CharSequence s) method
- Java – String contentEquals(StringBuffer sb) method
- Java – String copyValueOf(char[] data) method (2)
- Java – String endsWith(String suffix) method
- Java – String equals(Object anObject) method
- Java – String equalsIgnoreCase(Object anObject) method
- Java – String format(String format, Object… args) method
- Java – String getBytes() method (4)
- Java – String getChars() method
- Java – String hashCode() method
- Java – String indexOf() method (4)
- Java – String intern() method
- Java – String isEmpty() method
- Java – String join() method (2)
- Java – String lastIndexOf() method (4)
- Java – String length() method
- Java – String matches(String regex) method
- Java – String regionMatches() method (2)
- Java – String replace(char oldChar, char newChar) method
- Java – String replace(CharSequence target, CharSequence replacement) method
- Java – String replaceAll(String regex, String replacement) method
- Java – String replaceFirst(String regex, String replacement) method
- Java – String split(String regex) method
- Java – String split(String regex, int limit) method
- Java – String startsWith(String prefix) method
- Java – String startsWith(String prefix, int toffset) method
- Java – String subSequence(int beginIndex, int endIndex) method
- Java – String substring(int beginIndex) method
- Java – String substring(int beginIndex, int endIndex) method
- Java – String toCharArray() method
- Java – String toLowerCase() method (2)
- Java – String toUpperCase() method (2)
- Java – String toString() method
- Java – String trim() method
- Java – String valueOf() method (9)
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/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!