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.
Examples:
Input: char s[] = { ‘g’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ‘e’, ‘e’, ‘k’, ‘s’ }
Output: “geeksforgeeks”Input: char s[] = { ‘c’, ‘o’, ‘d’, ‘i’, ‘n’, ‘g’ }
Output: “coding”
Method 1: 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.
Below is the implementation of the above approach:
// Java program to demonstrate the // conversion of a char[] array // to a string public class GFG { // Function to convert a character // array to a string using the // constructor public static String toString(char[] a) { String string = new String(a); return string; } // Driver code public static void main(String args[]) { // Character array char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's' }; // Print final output of char arr[]->string System.out.println(toString(s)); } } |
Output:
geeksforgeeks
Method 2: 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.
Below is the implementation of the above approach:
// Java program to demonstrate the // conversion of a char[] array // to a string public class GFG { // Function to convert a character // array to a string using the // StringBuilder class public static String toString(char[] a) { StringBuilder sb = new StringBuilder(); //Using append() method to create a string for (int i = 0; i < a.length; i++) { sb.append(a[i]); } return sb.toString(); } // Driver code public static void main(String args[]) { // Defining character array char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's' }; System.out.println(toString(s)); } } |
Output:
geeksforgeeks
Method 3: 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. In short, the array is converted to a String.
Below is the implementation of the above approach:
// Java program to demonstrate the // conversion of a char[] array // to a string public class GFG { // Function to convert a character // array to a string using the // valueOf() method public static String toString(char[] a) { String string = String.valueOf(a); return string; } // Driver code public static void main(String args[]) { // Defining character array char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's' }; System.out.println(toString(s)); } } |
Output:
geeksforgeeks
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Convert character array to string in C++
- Java Program to convert Character Array to IntStream
- Difference between String and Character array in Java
- Convert an ArrayList of String to a String array in Java
- How to convert an Array to String in Java?
- Convert Set of String to Array of String in Java
- Convert the string into palindrome string by changing only one character.
- Number of ways to convert a character X to a string Y
- Minimum operations required to convert all characters of a String to a given Character
- How to convert given number to a character array
- Convert a List of String to a comma separated String in Java
- Convert a Set of String to a comma separated String in Java
- Java Program to get a character from a String
- Swap the first and last character of a string in Java
- How to find the first and last character of a string in Java
- Remove first and last character of a string in Java
- Count of number of given string in 2D character array
- Replace a character at a specific index in a String in Java
- Count occurrence of a given character in a string using Stream API in Java
- How to convert a String to an Int in Java?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

