Character[] array to String conversion in Java – 5 ways

In this article, we will discuss various ways to convert Character[] array to String in Java

 

What is the need of converting primitive char[] array or wrapper-type Character[] array to String ?

  • Generally, whenever we receive or send data from web application then it is passed in the form of String only
  • Sometimes for displaying purpose, a char[] array needs to be converted into string
  • This article explains about primitive char[] array or Character[] array wrapper-type to String conversion only, but we can do conversion for other types like int, double, float, long, boolean, etc. to String object
  • Note: Likewise, sometime String to char[] array conversion is also required

 

Various ways to convert Character[] array to String

  1. using Arrays.toString(chArray)
  2. using String.valueOf(chArray);
  3. using copyValueOf(chArray); method of String class
  4. create String object and then pass char[] array in the constructor {new String(chArray);}
  5. using append() method of StringBuffer & StringBuilder

 

Read String class in detail with example

 

Let us move forward and discuss all possible ways to convert Character[] array to String in Java

Way 1: Using Arrays.toString(chArray); method

  • This method can be used to convert primitive char[] array to String object

Method signature:

public static String toString(char[] a);

 

ConvertCharacterArrayIntoStringUsingToStringMethod.java

package in.bench.resources.character.array.to.string.conversion;

import java.util.Arrays;

public class ConvertCharacterArrayIntoStringUsingToStringMethod {

	public static void main(String[] args) {

		// primitive char[] array
		char[] chArray1 = {'B', 'e', 'n', 'c', 'h'};

		// 1. converting char[] array to String by using toString() method
		String str1 = Arrays.toString(chArray1);
		System.out.println("1. Converted String values are : " + str1);


		// primitive char[] array
		char[] chArray2 = new char[]{'R', 'e', 's', 'o', 'u', 'r', 'c', 'e', 's'};

		// 2. converting char[] array to String by using toString() method
		String str2 = Arrays.toString(chArray2);
		System.out.println("\n2. Converted String values are : " + str2);


		// Character object
		Character[] chArray3 = new Character[]{'G', 'o', 'o', 'g', 'l', 'e'};

		// 3. converting Character[] array object to String by using toString() method
		String str3 = Arrays.toString(chArray3);
		System.out.println("\n3. Converted String values are : " + str3);
	}
}

Output:

1. Converted String values are : [B, e, n, c, h]

2. Converted String values are : [R, e, s, o, u, r, c, e, s]

3. Converted String values are : [G, o, o, g, l, e]

 

Way 2: Using String.valueOf(chArray); method

  • This method can be used to convert primitive char[] array to String object

Method signature:

public static String valueOf(char data[]);

 

ConvertCharacterArrayIntoStringUsingValueOfMethod.java

package in.bench.resources.character.array.to.string.conversion;

public class ConvertCharacterArrayIntoStringUsingValueOfMethod {

	public static void main(String[] args) {

		// primitive char[] array
		char[] chArray1 = {'B', 'e', 'n', 'c', 'h'};

		// 1. converting char[] array to String by using toString() method
		String str1 = String.valueOf(chArray1);
		System.out.println("1. Converted primitive char[] array "
				+ "to String value is : " + str1);


		// primitive char[] array
		char[] chArray2 = new char[]{'R', 'e', 's', 'o', 'u', 'r', 'c', 'e', 's'};

		// 2. converting char[] array to String by using toString() method
		String str2 = String.valueOf(chArray2);
		System.out.println("\n2. Converted primitive char[] array "
				+ "to String value is : " + str2);
	}
}

Output:

1. Converted primitive char[] array to String value is : Bench

2. Converted primitive char[] array to String value is : Resources

 

Way 3: using copyValueOf(chArray); method of String class

  • This method can be used to convert primitive char[] array to String object

Method signature:

public static String copyValueOf(char data[]);

 

ConvertCharacterArrayIntoStringUsingCopyValueOfMethod.java

package in.bench.resources.character.array.to.string.conversion;

public class ConvertCharacterArrayIntoStringUsingCopyValueOfMethod {

	public static void main(String[] args) {

		// primitive char[] array
		char[] chArray1 = {'B', 'e', 'n', 'c', 'h'};

		// 1. converting char[] array to String by using copyValueOf() method
		String str1 = String.copyValueOf(chArray1);
		System.out.println("1. Converted String value is : " + str1);


		// primitive char[] array
		char[] chArray2 = new char[]{'R', 'e', 's', 'o', 'u', 'r', 'c', 'e', 's'};

		// 2. converting char[] array to String by using copyValueOf() method
		String str2 = String.copyValueOf(chArray2);
		System.out.println("\n2. Converted String value is : " + str2);
	}
}

Output:

1. Converted String value is : Bench

2. Converted String value is : Resources

 

Way 4: create String object and then pass char[] array in the constructor {new String(chArray);}

  • Here, we will create new String object and then pass char[] array object as constructor-argument
  • After creating new String object by passing char[] array, then invoke toString() method for converting char[] array to String object

Method signature:

public String(char value[]);

public String toString();

 

ConvertCharacterArrayIntoStringUsingObjectCreation.java

package in.bench.resources.character.array.to.string.conversion;

public class ConvertCharacterArrayIntoStringUsingObjectCreation {

	public static void main(String[] args) {

		// primitive char[] array
		char[] chArray1 = {'B', 'e', 'n', 'c', 'h'};

		// 1. converting char[] array to String by creating String object 
		String strObj1 = new String(chArray1);

		// and then invoke toString(); method
		String str1 = strObj1.toString();
		System.out.println("1. Converted String value is : " + str1);


		// primitive char[] array
		char[] chArray2 = new char[]{'R', 'e', 's', 'o', 'u', 'r', 'c', 'e', 's'};

		// 2. converting char[] array to String by creating String object
		String strObj2 = new String(chArray2);

		// and then invoke toString(); method
		String str2 = strObj2.toString();
		System.out.println("\n2. Converted String value is : " + str2);
	}
}

Output:

1. Converted String value is : Bench

2. Converted String value is : Resources

 

Way 5: Using append() method of StringBuffer & StringBuilder

  • This method can be used to convert primitive char[] array to String object by appending to StringBuffer or StringBuilder object
  • And then invoking toString() method

Syntax:

StringBuffer sb = sb.append(chArray);

String temp = sb.toString();

 

ConvertCharacterArrayIntoStringByAppending.java

package in.bench.resources.character.array.to.string.conversion;

public class ConvertCharacterArrayIntoStringByAppending {

	public static void main(String[] args) {

		// primitive char[] array
		char[] chArray1 = {'B', 'e', 'n', 'c', 'h'};

		// 1. converting char[] array to String by using append() method
		StringBuffer sb1 = new StringBuffer();
		String str1 = sb1.append(chArray1).toString();
		System.out.println("1. Converted String value is : " + str1);


		// primitive char[] array
		char[] chArray2 = new char[]{'R', 'e', 's', 'o', 'u', 'r', 'c', 'e', 's'};

		// 2. converting char[] array to String by using append() method
		StringBuffer sb2 = new StringBuffer();
		String str2 = sb2.append(chArray2).toString();
		System.out.println("\n2. Converted String value is : " + str2);
	}
}

Output:

1. Converted String value is : Bench

2. Converted String value is : Resources

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
https://docs.oracle.com/javase/tutorial/java/data/numberclasses.html
https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
https://docs.oracle.com/javase/tutorial/java/data/converting.html
https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
https://docs.oracle.com/javase/6/docs/api/java/lang/Character.html
https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html
https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html
https://docs.oracle.com/javase/tutorial/java/data/strings.html
https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html

 

Happy Coding !!
Happy Learning !!

String to Byte conversion in Java - 3 ways
String to char[] array conversion in Java - 4 ways