Convert Char to String Java With Examples

There are six ways for char to string java conversion. 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 :

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. 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 java char to String . 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.

Thanks for Reading!

Newsletter Updates

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

Leave a Reply