Tuesday, November 19, 2024
Google search engine
HomeData Modelling & AIConvert Character Array to String in Java

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. 

Illustrations:

Input  1 : char s[] = { ‘g’, ‘e’, ‘e’, ‘k’, ‘s’, ‘f’, ‘o’, ‘r’, ‘g’, ‘e’, ‘e’, ‘k’, ‘s’ } 

Output 1 : “neveropen” 

Input  2 : char s[] = { ‘c’, ‘o’, ‘d’, ‘i’, ‘n’, ‘g’ } 

Output 2 : “coding”

Methods:

  1. Using copyOf() method of Arrays class
  2. Using StringBuilder class
  3. Using valueOf() method of String class
  4. Using copyValueOf() method of String class
  5. 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




// Java Program to Convert Character Array to String
// Using copyOf() method ofArrays() Class
 
// Importing required classes
import java.util.*;
 
// Main class
class GFG {
 
    // Method 1
    // To convert a character
    // array to a string using the constructor
    public static String toString(char[] a)
    {
        // Creating object of String class
        String string = new String(a);
 
        return string;
    }
 
    // Method 2
    // Main driver method
    public static void main(String args[])
    {
 
        // Declaring and initializing a character array
        char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o',
                     'r', 'g', 'e', 'e', 'k', 's' };
 
        // Printing converted string from character array
        System.out.println(toString(s));
    }
}


Output: 
 

neveropen

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




// Java Program to Convert Character Array to String
// Using StringBuilder Class
 
// Importing required classes
import java.util.*;
 
// Main class
public class GFG {
 
    // Method 1
    // To convert a character array to a string
    // using the StringBuilder class
    public static String toString(char[] a)
    {
        // Creating object of String class
        StringBuilder sb = new StringBuilder();
 
        // Creating a string using append() method
        for (int i = 0; i < a.length; i++) {
            sb.append(a[i]);
        }
 
        return sb.toString();
    }
 
    // Method 2
    // Main driver method
    public static void main(String args[])
    {
 
        // Declaring and initialzaing input character array
        char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o',
                     'r', 'g', 'e', 'e', 'k', 's' };
 
        // Printing the string
        // corresponding to character array
        System.out.println(toString(s));
    }
}


Output

neveropen

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




// Java Program to Convert Character Array to String
// Using valueOf() method of String Class
 
// Importing required classes
import java.util.*;
 
// Main class
class GFG {
 
    // Method 1
    // To convert a character array to string
    // using the valueOf() method
    public static String toString(char[] a)
    {
        // Creating an object of String class
        String string = String.valueOf(a);
 
        return string;
    }
 
    // Method 2
    // Main driver method
    public static void main(String args[])
    {
 
        // Declaring and initializing input character array
        char s[] = { 'g', 'e', 'e', 'k', 's', 'f', 'o',
                     'r', 'g', 'e', 'e', 'k', 's' };
 
        // Print the corresponding string to
        // character array
        System.out.println(toString(s));
    }
}


Output

neveropen

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




// Java Program to Convert Character Array to String
// Using copyValueOf() method of String Class
 
// Importing String class
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing input character array
        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);
 
        // Printing the converted string corresponding
        // character array
        System.out.print(str);
    }
}


Output

neveropen

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




// Java Program to Convert a Character array to String
// Using Collectors in Streams in Java8
 
// Importing Collectos and Stream classes
// from java.util.stream package
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Custom input character array
        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());
 
        // Printing the stream received from Collectors
        System.out.println(str);
    }
}


Output

neveropen

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments