Convert Char to String Java With Examples

There are six ways to convert char to string in java. Suppose there is a char ‘a’ and you need to convert it to equivalent String e.g. “a” then you may use any of the following six methods to convert the primitive char value to String in Java :

1) String.valueOf()
2) String concatenation
3) Character.toString()
4) String constructor with char array
5) Character wrapper class using toString method
6) String.valueOf(char [])

Char to String using String.valueOf()

This is the most efficient method for converting char to String in Java. This is one of the way to convert a char primitive to String object. The valueOf() method can also be used to convert String to int and String to double in Java. Below is the example of using this method to convert char to String

char bChar = 'b';
String bStr = String.valueOf(bChar);
System.out.println("char to String using String.valueOf() : " + bStr); // "b"

Char to String using concatenation

It is the easiest way to convert a char to String in Java. You have to concatenate given character with empty String as shown in the following example

char aChar= 'b';
String aStr = "" + aChar;
System.out.println("char to String using concatenation : " + aStr);   // b

Char to String using Character.toString()

It is the third example to convert a char value to a String in Java. Here in this example we have used Character.toString() method.

char bChar= 'b';
String bStr = Character.toString(bChar);
System.out.println("char to String using Character.toString() : " + bStr); // b

String constructor with char array

This is one of the way to convert a char value to String object in Java. In this example, we have wrapped the single character into a character array and passed it to a String constructor which accepts a char array as shown below:

String str = new String(new char[]{'k'});
System.out.println("char to String by using new String(char array) : " + str);

Character wrapper class using toString() method

Here we have used Character class to convert char into String.

char kc = 'k';
String kStr = new Character(kc).toString();
System.out.println("char to String using new Character() + toString : " + kStr);

char to String using String.valueOf(char [])

valueOf() method is overloaded method of String class which can accept char array too.

String kStr = String.valueOf(new char[]{'k'});
System.out.println("char to String using String.valueOf(char[] array) : " + kStr);

Conclusion

You have learnt what are the various ways of converting char to String java. You can use as per your need in your program.

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply