- Given char array in java.
- We would like to convert char array to string using following methods.
- Java 8 Stream lambda
- String class constructor
- valueOf method of String class
- copyValueOf method of String class
- append method of StringBuilder class
Example: Char array to string
Example : Given char array as follows:
chArray[] = {'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's'};
The output of program should be: Tutorials
Method 1: char array to string using Java 8 Stream lambda
class CharArrayToStringJava8 {
private static void charArrayToString(char[] chArray) {
//Java8 Stream lambda
String streamOutput = IntStream
.range(0, chArray.length)
.mapToObj(index -> chArray[index])
.map(String::valueOf)
.collect(Collectors.joining());
System.out.println("Conversion using Java 8 stream: ");
System.out.print(streamOutput);
}
public static void main(String[] args) {
char chArray[] = {'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's'};
charArrayToString(chArray);
}
}
Output: char array to string using Java 8 Stream lambda
Conversion using Java 8 stream lambda: Tutorials
Method 2: char array to string using String constructor
- String constructor takes char array as an argument, which would convert char array to string.
Method Name |
Description |
String(char[] value) |
Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. |
Program: convert char array to string using String constructor
class CharArrayToStringUsingConstructor {
private static void charArrayToString(char[] chArray) {
//Convert using String constructor
String output = new String(chArray);
System.out.println("Conversion using String Constructor: ");
System.out.print(output);
}
public static void main(String[] args) {
char chArray[] = {'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's'};
charArrayToString(chArray);
}
}
Output: char array to string using String constructor
Conversion using String Constructor: Tutorials
Method 3: char array to string using valueOf method
- String class provide valueOf method which converts char buffer to String.
Method Name |
Description |
String valueOf(char[] data) |
Returns the string representation of the char array argument. |
Program: convert char array to String using valueOf method
class CharArrayToStringUsingValueOf {
private static void charArrayToString(char[] chArray) {
//Convert using string copyOf
String strValueOf = String.valueOf(chArray);
System.out.println("Conversion using valueOf method: ");
System.out.print(strValueOf);
}
public static void main(String[] args) {
char chArray[] = {'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's'};
charArrayToString(chArray);
}
}
Output: char array to string using valueOf method
Conversion using valueOf method: Tutorials
Method 4: char array to string using copyValueOf method
- String class provide copyValueOf method which converts char buffer to String.
Method Name |
Description |
String copyValueOf(char[] data) |
Return a String that contains the characters of the character array. |
Program: convert char array to String using valueOf method
class CharArrayToStringUsingCopyValueOf {
private static void charArrayToString(char[] chArray) {
//Convert using string copyOf
String strValueOf = String.copyValueOf(chArray);
System.out.print("Conversion using copyValueOf method: ");
System.out.print(strValueOf);
}
public static void main(String[] args) {
char chArray[] = {'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's'};
charArrayToString(chArray);
}
}
Output: char array to string using valueOf method
Conversion using copyValueOf method: Tutorials
Method 5: StringBuilder append method to convert char array to string
- StringBuilder class provide append method which converts char buffer to StringBuilder.
Method Name |
Description |
StringBuilder append(char[] str) |
Appends the string representation of the char array argument to this sequence. |
Program: char array to String using StringBuilder class
class CharArrayToStringUsingStringBuilder {
private static void charArrayToString(char[] chArray) {
//convert using StringBuilder
StringBuilder builder = new StringBuilder();
builder.append(chArray);
System.out.print("Conversion using StringBuilder method: ");
System.out.print(builder.toString());
}
public static void main(String[] args) {
char chArray[] = {'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', 's'};
charArrayToString(chArray);
}
}
Output: char array to string using StringBuilder append
Conversion using StringBuilder method: Tutorials