Looking to convert Java Char array to String?

Lets explore numbers of ways to achieve it using Java programming language.

Char array can be converted to String and vice versa.

Allocate a String that exhibits the sequence of characters present in a char array.

As String is immutable in Java, the consecutive modification of the character array does not impact the allocated String.

Using String Constructor

The String class provides inbuilt constructor which accepts a char array as an argument.

public static void main(String[] args) {
	try {
	 char[] charArray = { 'W', 'E', 'L', 'C', 'O', 'M', 'E'};
	 String value = new String(charArray);
	 System.out.println("String Class Value is: " +value);		 
	} catch (Exception e) {
		e.printStackTrace();
	}

}

Output:
String Class Value is: WELCOME

 

With StringBuilder

public static void main(String[] args) {
try {
  final char[][] arrayCharArray = {{'w','e'},{'l','c'},{'o','m','e'}};    
  StringBuilder sb = new StringBuilder();
  for (char[] childArray : arrayCharArray) {
   sb.append(childArray);
    }
    System.out.println("After Forming as String: " +sb);		 
    } catch (Exception e) {
		e.printStackTrace();
    }

}

Output:
After Forming as String: welcome

 

Using String.valueOf() Method

The String class provides valueOf() method which converts to string directly.

public static void main(String[] args) {
 try {
   char[] charArray = { 'W', 'E', 'L', 'C', 'O', 'M', 'E'};
   String value = String.valueOf(charArray);
   System.out.println("String Value is: " +value);		 
     } catch (Exception e) {
	   e.printStackTrace();
     }

}

Output:
String Value is: WELCOME

 

Using String.copyValueOf() Method

The String class provides another method copyValueOf() method.

This will copy char array to string in Java.

This method is very similar to valueOf() method.

public static void main(String[] args) {
try {
  char[] charArray = { 'W', 'E', 'L', 'C', 'O', 'M', 'E'};
  String value = String.copyValueOf(charArray);
  System.out.println("String copyValueOf is: " +value);		 
    } catch (Exception e) {
       e.printStackTrace();
    }

}

Output:
String copyValueOf is: WELCOME

 

Utilizing Java 8 Streams

We can utilize the Collectors.joining() method to form a String.

stream method defined in the following way.

public static void main(String[] args) {
try {
  Character[] charArray = { 'W', 'E', 'L', 'C', 'O', 'M', 'E'};
  Stream<Character> charStream = Arrays.stream(charArray);
  String value = charStream.map(String::valueOf).collect(Collectors.joining());	    
  System.out.println("String Java 8 Stream is: " +value);		 
    } catch (Exception e) {
	   e.printStackTrace();
    }

}

Output:
String Java 8 Stream is: WELCOME

 

Using Google’s Guava Joiner Method

We can use the Guava Common Base Joiner method, to convert from Character array to delimiter string value.

The import package is import com.google.common.base.Joiner;

public static void main(String[] args) {
 try {
	Character[] charArray = { 'W', 'E', 'L', 'C', 'O', 'M', 'E'};
	String value = Joiner.on("-").join(charArray);
	System.out.println("Joiner Value is: " +value);		 
     } catch (Exception e) {
        e.printStackTrace();
     }

}

Output:
Joiner Value is: W-E-L-C-O-M-E

 

Convert a Byte Array to String in Java

As we know byte[] stores binary data and String stores text data.

So lets see how to convert a byte array to string with character encoding and without character encoding.

With Character Encoding:

It supports both UNICODE and ASCII format.

In this case, lets use StandardCharsets.UTF_8 to specify the type of character encoding type.

This tells how to encode the input characters into the sequence of bytes.

public static void main(String[] args) throws IOException
	{
		String value = "Byte Array";
		byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
		// Now Create a string from a byte array with "UTF-8" encoding
		String result = new String(bytes, StandardCharsets.UTF_8);
		System.out.println(result);
	}
	

Output:	
Byte Array

Without Character Encoding:

Lets convert byte array to string without specifying the character encoding.

So here we declared an string variable and converted into bytes using getBytes() method.

Finally passed the bytes to String object instance to achieve the desired output.

public static void main(String[] args) throws IOException
	{
		String value = "Byte Array";
		byte[] bytes = value.getBytes();
		String result = new String(bytes);
		System.out.println(result);
	}
	

Output:	
Byte Array


You May Also Love,

Iterate through the characters of a string in Java

Lets use for loop function to iterate the number of characters of a String.

Apply charAt() method to retrieve each character and check it.

public static void main(String[] args) throws IOException
	{
		String value = "char";
		for (int i = 0; i < value.length(); i++){
		    char result = value.charAt(i);
		    System.out.println("printing : "+result);
		}

	}
	

Output:	
printing : c
printing : h
printing : a
printing : r

Lets look at efficient way of using for loop function with below code snippet.

public static void main(String[] args) throws IOException
	{
		String value = "char";
		for(char result : value.toCharArray()) {
			System.out.println("printing : "+result);
		}
	}	

Output:
printing : c
printing : h
printing : a
printing : r



Tip:
Unlike C, There’s no such entity as NULL in Java. Null is not a valid value for a char variable.
A char can have a integer value of zero, which has no particular significance.

To conclude this java tutorial, we covered various examples on converting char Array to a String in Java.

Rate this post

Pin It on Pinterest

Share This