Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character “\0”. A character array can be converted to a string and vice versa. In the previous article, we have already discussed how to convert a string to a character array. In this article, we will discuss how to convert a character array to a string.
Illustrations:
Input 1 : char s[] = { ‘g’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ‘e’, ‘e’, ‘k’, ‘s’ }
Output 1 : “geeksforgeeks”
Input 2 : char s[] = { ‘c’, ‘o’, ‘d’, ‘i’, ‘n’, ‘g’ }
Output 2 : “coding”
Methods:
- Using copyOf() method of Arrays class
- Using StringBuilder class
- Using valueOf() method of String class
- Using copyValueOf() method of String class
- Using Collectors in Streams
Now let us discuss each of the methods in detail alongside implementing them with help of a clean java program.
Method 1: Using copyOf() method of Array class
The given character can be passed into the String constructor. By default, the character array contents are copied using the Arrays.copyOf() method present in the Arrays class.
Example:
Java
import java.util.*;
class GFG {
public static String toString( char [] a)
{
String string = new String(a);
return string;
}
public static void main(String args[])
{
char s[] = { 'g' , 'e' , 'e' , 'k' , 's' , 'f' , 'o' ,
'r' , 'g' , 'e' , 'e' , 'k' , 's' };
System.out.println(toString(s));
}
}
|
Output:
geeksforgeeks
Method 2: Using StringBuilder class
Another way to convert a character array to a string is to use the StringBuilder class. Since a StringBuilder is a mutable class, therefore, the idea is to iterate through the character array and append each character at the end of the string. Finally, the string contains the string form of the characters.
Example:
Java
import java.util.*;
public class GFG {
public static String toString( char [] a)
{
StringBuilder sb = new StringBuilder();
for ( int i = 0 ; i < a.length; i++) {
sb.append(a[i]);
}
return sb.toString();
}
public static void main(String args[])
{
char s[] = { 'g' , 'e' , 'e' , 'k' , 's' , 'f' , 'o' ,
'r' , 'g' , 'e' , 'e' , 'k' , 's' };
System.out.println(toString(s));
}
}
|
Method 3: Using valueOf() method of String class
Another way to convert a character array to a string is to use the valueOf() method present in the String class. This method inherently converts the character array to a format where the entire value of the characters present in the array is displayed. This method generally converts int, float, double, char, boolean, and even object to a string. Here we will achieve the goal by converting our character array to string.
Example:
Java
import java.util.*;
class GFG {
public static String toString( char [] a)
{
String string = String.valueOf(a);
return string;
}
public static void main(String args[])
{
char s[] = { 'g' , 'e' , 'e' , 'k' , 's' , 'f' , 'o' ,
'r' , 'g' , 'e' , 'e' , 'k' , 's' };
System.out.println(toString(s));
}
}
|
Method 4: Using copyValueOf() method of String class
The contents from the character array are copied and subsequently modified without affecting the string to be returned, hence this method also enables us to convert the character array to a string which can be perceived even better from the example provided below as follows.
Example:
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
char [] arr = { 'g' , 'e' , 'e' , 'k' , 's' , 'f' , 'o' ,
'r' , 'g' , 'e' , 'e' , 'k' , 's' };
String str = String.copyValueOf(arr);
System.out.print(str);
}
}
|
Method 5: Using Collectors in Streams
With the introduction of streams in java8, we straight away use Collectors in streams to modify our character input array elements and later uses joining() method and return a single string and print it.
Example:
Java
import java.util.stream.Collectors;
import java.util.stream.Stream;
class GFG {
public static void main(String[] args)
{
char [] charr = { 'g' , 'e' , 'e' , 'k' , 's' , 'f' , 'o' ,
'r' , 'g' , 'e' , 'e' , 'k' , 's' };
String str = Stream.of(charr)
.map(arr -> new String(arr))
.collect(Collectors.joining());
System.out.println(str);
}
}
|