Convert Character Array To String In Java

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.