Open In App

How to Convert Character Array to String in Java?

Last Updated : 20 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 this article, we will discuss how to convert a character array to a string. 

Examples:

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”

Program:

Java
// Convert Character Array to String
// Using copyOf() method ofArrays() Class
import java.util.*;

class GFG {

    // To convert a character
    // array to a string using the constructor
    public static String toString(char[] a){
        String str = new String(a);
        return str;
    }

    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

Here we are 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

Other Methods to Convert Character Array to String

  1. Using StringBuilder class
  2. Using valueOf() method of String class
  3. Using copyValueOf() method of String class
  4. Using Collectors in Streams

1. 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
// Convert Character Array to String
// Using StringBuilder Class
import java.util.*;

public class GFG {

    // To convert a character array to a string
    // using the StringBuilder class
    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));
    }
}

Output
geeksforgeeks

2. 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
// Convert Character Array to String
// Using valueOf() method
import java.util.*;

class GFG {

    // To convert a character array to string
    // using the valueOf() method
    public static String toString(char[] a)
    {  
        String string = String.valueOf(a);
        return string;
    }

    // Main driver method
    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

3. Using copyValueOf() method of String class

Using copyValueOf() method of String class to use contents from the character array that 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
// Convert Character Array to String
// Using copyValueOf() method

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' };

        // Storing it in a string
        // using copyValueOf() over string
        String str = String.copyValueOf(arr);

        System.out.print(str);
    }
}

Output
geeksforgeeks

4. 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
// Convert a Character array to String
// Using Collectors in Streams in Java8
import java.util.stream.Collectors;
import java.util.stream.Stream;

class Main {
  
    public static void main(String[] args)
    {
        char[] charr = { 'g', 'e', 'e', 'k', 's', 'f', 'o',
                         'r', 'g', 'e', 'e', 'k', 's' };

        // Using collectors to collect array elements and
        // later using joining method to return a single
        // string
        String str = Stream.of(charr)
                         .map(arr -> new String(arr))
                         .collect(Collectors.joining());

        System.out.println(str);
    }
}

Output
geeksforgeeks


Next Article
Article Tags :
Practice Tags :

Similar Reads