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 Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Convert the string into palindrome string by changing only one character
- Convert character array to string in C++
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
- Replace every character of a string by a different character
- Map every character of one string to another such that all occurrences are mapped to the same character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Replace all occurrences of character X with character Y in given string
- Count of substrings having the most frequent character in the string as first character
- Java Program to convert Character Array to IntStream
- Nearest smaller character to a character K from a Sorted Array
- Number of ways to convert a character X to a string Y
- Convert given string to another by minimum replacements of subsequences by its smallest character
- Minimum operations required to convert all characters of a String to a given Character
- Convert vowels into upper case character in a given string
- Convert Set of String to Array of String in Java
- Convert an ArrayList of String to a String array in Java
- How to convert given number to a character array
- Longest Common Prefix using Character by Character Matching
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.

