Character to String conversion in Java – 6 ways

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

 

What is the need of converting primitive char or Character wrapper-type 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 value needs to be converted into string
  • This article explains about primitive char or Character 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 Character conversion is also required

 

Various ways to convert Character to String

  1. using Character.toString(chValue);
  2. using String.valueOf(chValue);
  3. using String.format(type, chValue);
  4. create Character object and then convert to String using toString() method {new Character(chValue).toString();}
  5. adding double quotes (“”) to character value {i.e.; “” + chValue;}
  6. using append() method of StringBuffer & StringBuilder

 

Auto-boxing and un-boxing feature from Java 1.5 version:

  • charAt() method returns primitive char data-type, but it can be easily used as Character wrapper-type
  • as auto-boxing feature helps to convert primitive data-type to respective wrapper-types
  • Newly created Character wrapper-type object can be easily converted to primitive char data-type
  • let’s see one example on this auto-boxing feature, at the very end

 

Read String class in detail with example

 

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

Way 1: Using Character.toString(chValue); method

  • This method can be used to convert primitive char data-type or Character wrapper-type to String object

Method signature:

public static String toString(char c);

 

ConvertCharacterIntoStringUsingToStringMethod.java

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

public class ConvertCharacterIntoStringUsingToStringMethod {

	public static void main(String[] args) {

		// primitive char data-type (upper-case)
		char chValue1 = 'A';

		// 1. converting char to String by using toString() method
		String str1 = Character.toString(chValue1);
		System.out.println("1. Converted primitive char to String value is : "
				+ str1);


		// primitive char data-type (lower-case)
		char chValue2 = 't';

		// 2. converting char to String by using toString() method
		String str2 = Character.toString(chValue2);
		System.out.println("\n2. Converted primitive char to String value is : "
				+ str2);


		// Character object
		Character chValue3 = new Character('S');

		// 3. converting Boolean object to String by using toString() method
		String str3 = Character.toString(chValue3);
		System.out.println("\n3. Converted Character object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive char to String value is : A

2. Converted primitive char to String value is : t

3. Converted Character object to String value is : S

 

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

  • This method can be used to convert primitive char data-type or Character wrapper-type to String object

Method signature:

public static String valueOf(char c);

 

ConvertCharacterIntoStringUsingValueOfMethod.java

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

public class ConvertCharacterIntoStringUsingValueOfMethod {

	public static void main(String[] args) {

		// primitive char data-type (upper-case)
		char chValue1 = 'B';

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


		// primitive char data-type (lower-case)
		char chValue2 = 'f';

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


		// Character object
		Character chValue3 = new Character('p');

		// 3. converting Character object to String by using valueOf() method
		String str3 = String.valueOf(chValue3);
		System.out.println("\n3. Converted Character object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive char to String value is : B

2. Converted primitive char to String value is : f

3. Converted Character object to String value is : p

 

Way 3: Using String.format(type, number); method

  • This method can be used to convert primitive char data-type or Character wrapper-type to String object

Method signature:

public static String format(String format, Object... args);

 

ConvertCharacterIntoStringUsingFormatMethod.java

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

public class ConvertCharacterIntoStringUsingFormatMethod {

	public static void main(String[] args) {

		// primitive char data-type (upper-case)
		char chValue1 = 'C';

		// 1. converting char to String by using format() method
		String str1 = String.format("%c", chValue1);
		System.out.println("1. Converted primitive char to String value is : "
				+ str1);


		// primitive char data-type (lower-case)
		char chValue2 = 'e';

		// 2. converting char to String by using format() method
		String str2 = String.format("%c", chValue2);
		System.out.println("\n2. Converted primitive char to String value is : "
				+ str2);


		// Character object
		Character chValue3 = new Character('N');

		// 3. converting Boolean object to String by using format() method
		String str3 = String.format("%c", chValue3);
		System.out.println("\n3. Converted Character object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive char to String value is : C

2. Converted primitive char to String value is : e

3. Converted Character object to String value is : N

 

Way 4: create Character object and then convert to String using toString() method {new Character(boolValue).toString();}

  • Here, we will create new Character object with primitive char value as constructor-argument
  • After creating new Character object by passing primitive char value, then invoke toString() method for converting primitive char data-type to String object

Method signature:

public Character(char value);

public String toString();

 

ConvertCharacterIntoStringUsingObjectCreation.java

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

public class ConvertCharacterIntoStringUsingObjectCreation {

	public static void main(String[] args) {

		// primitive char data-type (upper-case)
		char chValue1 = 'D';

		// 1. converting char to String by creating Long object 
		Character chObj1 = new Character(chValue1);

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


		// primitive char data-type (lower-case)
		char chValue2 = 'g';

		// 2. converting char to String by creating Long object
		Character boolObj2 = new Character(chValue2);

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


		// Character object
		Character chValue3 = new Character('k');

		// 3. converting Character object to String by creating Long object
		String str3 = chValue3.toString();
		System.out.println("\n3. Converted Character object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive char to String value is : D

2. Converted primitive char to String value is : g

3. Converted Character object to String value is : k

 

Way 5: adding double quotes (“”) to Character value

Note: for this conversion to work, double-quotes (“”) must be added at the start of the concatenation

Syntax:

String temp = “” + chValue;

 

ConvertCharacterIntoStringByAddingDoubleQuotes.java

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

public class ConvertCharacterIntoStringByAddingDoubleQuotes {

	public static void main(String[] args) {

		// primitive char data-type (upper-case)
		char chValue1 = 'E';

		// 1. converting char to String by adding ""
		String str1 = "" + chValue1;
		System.out.println("1. Converted primitive char to String value is : "
				+ str1);


		// primitive char data-type (lower-case)
		char chValue2 = 'h';

		// 2. converting char to String by adding ""
		String str2 = "" + chValue2;
		System.out.println("\n2. Converted primitive char to String value is : "
				+ str2);


		// Character object
		Character chValue3 = new Character('W');

		// 3. converting Boolean object to String  by adding ""
		String str3 = "" + chValue3;
		System.out.println("\n3. Converted Character object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive char to String value is : E

2. Converted primitive char to String value is : h

3. Converted Character object to String value is : W

 

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

  • This method can be used to convert primitive char data-type to String object by appending to StringBuffer or StringBuilder object
  • And then invoking toString() method

Syntax:

StringBuffer sb = sb.append(chValue);

String temp = sb.toString();

 

ConvertCharacterIntoStringByAppending.java

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

public class ConvertCharacterIntoStringByAppending {

	public static void main(String[] args) {

		// primitive char data-type (upper-case)
		char chValue1 = 'F';

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


		// primitive char data-type (lower-case)
		char chValue2 = 'v';

		// 2. converting boolean to String by using append() method
		StringBuffer sb2 = new StringBuffer();
		String str2 = sb2.append(chValue2).toString();
		System.out.println("\n2. Converted primitive char to String value is : "
				+ str2);


		// Character object
		Character chValue3 = new Character('l');

		// 3. converting Character object to String by using append() method
		StringBuffer sb3 = new StringBuffer();
		String str3 = sb3.append(chValue3).toString();
		System.out.println("\n3. Converted Character object to String value is : "
				+ str3);
	}
}

Output:

1. Converted primitive char to String value is : F

2. Converted primitive char to String value is : v

3. Converted Character object to String value is : l

 

What if we want to convert to Character wrapper-type ? 

  • Auto-boxing feature available from Java 1.5 version
  • So, converting primitive data-type to respective wrapper-type can easily be done, by directly assigning
  • Let’s see one example based on this auto-boxing feature

AutoBoxingFeatureForCharConversion.java

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

public class AutoBoxingFeatureForCharConversion {

	public static void main(String[] args) {

		// String with upper-case
		String str1 = "C";

		// converting String to char
		char chValue1 = str1.charAt(0);

		// 1. Auto-Boxing - converting char to Character
		Character chAutoBoxing = chValue1;
		System.out.println("1. Auto-Boxing : " + chAutoBoxing);


		// String with lower-case
		String str2 = "z";

		// converting String to Character
		Character chValue2 = new Character(str2.charAt(0));

		// 2. Un-Boxing - converting Character to char
		char chUnBoxing = chValue2;
		System.out.println("\n2. Un-Boxing   : " + chUnBoxing);
	}
}

Output:

1. Auto-Boxing : C

2. Un-Boxing   : z

 

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 char[] array conversion in Java - 4 ways
String to char conversion in Java