Char Array To String Java

Char Array To String Java | Here, we will discuss how to convert a char array to a string in Java. We have already discussed how to convert string to character array in Java using the native method and by using the String.toCharArray() method.

See the below example so that you may get some idea to convert character array to string.
1. Character array:- [‘H’, ‘e’, ‘l’, ‘l’, ‘o’]
String:- “Hello”
2. Character array:- [‘K’, ‘n’, ‘o’, ‘w’, ‘ ‘ , ‘P’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’]
String:- “Know Program”

To convert char array to string in Java we have five different methods and they are as follows:-
1. By using the String class constructor.
2. By Using the valueOf() method of the String class.
3. By Using the copyValueOf() method of the String class.
4. By Using the StringBuilder or StringBuffer class.
5. By Using the Collectors in Streams.

Java Char Array To String Using String Constructor

Java String class contains the following constructors to convert char array to string:-

  1. public String(char value[ ]):- It allocates a new String so that it represents the sequence of characters currently contained in the character array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.
  2. public String(char value[ ], int offset, int count):- It allocates a new String that contains characters from a subarray of the character array argument. The “offset” argument is the index of the first character of the subarray and the “count” argument specifies the length of the subarray. The contents of the subarray are copied; subsequent modification of the character array does not affect the newly created string.

In the above constructors, the first one is used to convert the entire char array into a string whereas the second one is used to convert only the given portion of the char array to a string object. Let us see the Java program to demonstrate how to convert a char array to a string in Java using the String class constructor:-

import java.util.Arrays;

public class Main {
   public static void main(String args[]) {
      char charArray[] = { 'J', 'a', 'v', 'a', ' ', 
            'P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g', ' ', 
            'L', 'a', 'n', 'g', 'u', 'a', 'g', 'e' };
      String str1 = new String(charArray);
      String str2 = new String(charArray, 5, 11);

      System.out.println("Char array: " + Arrays.toString(charArray));
      System.out.println("String1: " + str1);
      System.out.println("String2: " + str2);
   }
}

Output:-

Char array: [J, a, v, a, , P, r, o, g, r, a, m, m, i, n, g, , L, a, n, g, u, a, g, e]
String1: Java Programming Language
String2: Programming

Char Array To String Java using String.valueOf()

Java string class also contains multiple overloaded forms of the valueOf() method which is used to convert different primitive data types to string objects. It contains the following overloaded forms of valueOf() to convert char array to string.

  1. public static String valueOf(char data[ ]):- Returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the returned string.
  2. public static String valueOf(char data[ ], int offset, int count):- Returns the string representation of a specific subarray of the char array argument. The “offset” argument is the index of the first character of the subarray. The “count” argument specifies the length of the subarray. The contents of the subarray are copied; subsequent modification of the character array does not affect the returned string.

Let us see the Java program to demonstrate how to convert a char array to a string in Java using the String.valueOf() method:-

import java.util.Arrays;

public class Main {
   public static void main(String args[]) {
      char charArray[] = { 'J', 'a', 'v', 'a', ' ', 
            'P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g', ' ', 
            'L', 'a', 'n', 'g', 'u', 'a', 'g', 'e' };
      String str1 = String.valueOf(charArray);
      String str2 = String.valueOf(charArray, 17, 8);

      System.out.println("Char array: " + Arrays.toString(charArray));
      System.out.println("String1: " + str1);
      System.out.println("String2: " + str2);
   }
}

Output:-

Char array: [J, a, v, a, , P, r, o, g, r, a, m, m, i, n, g, , L, a, n, g, u, a, g, e]
String1: Java Programming Language
String2: Language

Java Char Array To String using String.copyValueOf()

Similar to valueOf() method Java String class also contains the copyValueOf() method, and it has also two following forms:-

  • public static String copyValueOf(char data[ ]):- It is equivalent to calling String.valueOf(char[ ]) method.
  • public static String copyValueOf(char data[ ], int offset, int count):- It is equivalent to calling String.valueOf(char[ ], int, int)

Let us see the Java program to demonstrate how to convert a char array to a string in Java using the String.copyValueOf() Method:-

import java.util.Arrays;

public class Main {
   public static void main(String args[]) {
      char charArray[] = { 'J', 'a', 'v', 'a', ' ', 
            'P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g', ' ', 
            'L', 'a', 'n', 'g', 'u', 'a', 'g', 'e' };
      String str1 = String.copyValueOf(charArray);
      String str2 = String.copyValueOf(charArray, 17, 8);

      System.out.println("Char array: " + Arrays.toString(charArray));
      System.out.println("String1: " + str1);
      System.out.println("String2: " + str2);
   }
}

Output:-

Char array: [J, a, v, a, , P, r, o, g, r, a, m, m, i, n, g, , L, a, n, g, u, a, g, e]
String1: Java Programming Language
String2: Language

Char Array To String Java using StringBuilder or StringBuffer class

We can take the help of the StringBuilder or StringBuffer class to convert char array to string in Java. We will iterate through the array of characters and append the StringBuilder or StringBuffer object. Later we convert the StringBuilder or StringBuffer object to String. Let us see it through an example:-

import java.util.Arrays;

public class Main {
   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 charArray[] = { 'K', 'n', 'o', 'w', ' ',
                        'P', 'r', 'o', 'g', 'r', 'a', 'm' };
      String string = toString(charArray);
      System.out.println("Array: " + Arrays.toString(charArray));
      System.out.println("String: " + string);
   }
}

Output:-

Array: [K, n, o, w, , P, r, o, g, r, a, m]
String: Know Program

The above program uses the StringBuilder class, let us see the same program using the StringBuffer class.

import java.util.Arrays;

public class Main {
   public static String toString(char[] a) {
      StringBuffer sb = new StringBuffer();
      for (int i = 0; i < a.length; i++) {
         sb.append(a[i]);
      }
      return sb.toString();
   }

   public static void main(String args[]) {
      char charArray[] = { 'K', 'n', 'o', 'w', ' ',
                        'P', 'r', 'o', 'g', 'r', 'a', 'm' };
      String string = toString(charArray);
      System.out.println("Array: " + Arrays.toString(charArray));
      System.out.println("String: " + string);
   }
}

Output:-

Array: [K, n, o, w, , P, r, o, g, r, a, m]
String: Know Program

Convert Char Array To String Java Using Streams

We can also convert char array to string in Java using streams. Java program to demonstrate how to convert a char array to a string in Java using streams:-

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
   public static void main(String[] args) {
      char[] ch = { 'J', 'a', 'v', 'a', ' ', 
               'P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g', ' ', 
               'L', 'a', 'n', 'g', 'u', 'a', 'g', 'e' };
      String string = Stream.of(ch)
                             .map(arr -> new String(arr))
                             .collect(Collectors.joining());
      System.out.println("Array: " + Arrays.toString(ch));
      System.out.println("String: " + string);
   }
}

Output:-

Array: [J, a, v, a, , P, r, o, g, r, a, m, m, i, n, g, , L, a, n, g, u, a, g, e]
String: Java Programming Language

Get notes to make your learning process easy. These are specially designed for beginners who want to learn coding through simple words, programs, and examples. You can use it as your reference and for revision purposes.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.