Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a char to a String?

I have a char and I need a String. How do I convert from one to the other?

like image 808
Landon Kuhn Avatar asked Nov 17 '11 18:11

Landon Kuhn


People also ask

Can a char be a string?

A String containing a single character is not the same as a char. They both can be declared using quotes (single quotes for chars and double quotes for Strings), but they are very different. At a high level, a way to think about it is that a String is an Object that allows you to operate on a sequence of chars.

Can you use toString on char?

toString(char c) returns a String object representing the specified char. The result is a string of length 1 consisting solely of the specified char.

Can you type cast char to string java?

We can convert char to String in java using String. valueOf(char) method of String class and Character. toString(char) method of Character class.


2 Answers

You can use Character.toString(char). Note that this method simply returns a call to String.valueOf(char), which also works.

As others have noted, string concatenation works as a shortcut as well:

String s = "" + 's'; 

But this compiles down to:

String s = new StringBuilder().append("").append('s').toString(); 

which is less efficient because the StringBuilder is backed by a char[] (over-allocated by StringBuilder() to 16), only for that array to be defensively copied by the resulting String.

String.valueOf(char) "gets in the back door" by wrapping the char in a single-element array and passing it to the package private constructor String(char[], boolean), which avoids the array copy.

like image 127
Paul Bellora Avatar answered Oct 08 '22 03:10

Paul Bellora


I've got of the following five six methods to do it.

// Method #1 String stringValueOf = String.valueOf('c'); // most efficient  // Method #2 String stringValueOfCharArray = String.valueOf(new char[]{x});  // Method #3 String characterToString = Character.toString('c');  // Method #4 String characterObjectToString = new Character('c').toString();  // Method #5 // Although this approach seems very simple,  // this is less efficient because the concatenation // expands to a StringBuilder. String concatBlankString = 'c' + "";  // Method #6 String fromCharArray = new String(new char[]{x}); 

Note: Character.toString(char) returns String.valueOf(char). So effectively both are same.

String.valueOf(char[] value) invokes new String(char[] value), which in turn sets the value char array.

public String(char value[]) {     this.value = Arrays.copyOf(value, value.length); } 

On the other hand String.valueOf(char value) invokes the following package private constructor.

String(char[] value, boolean share) {     // assert share : "unshared not supported";     this.value = value; } 

Source code from String.java in Java 8 source code

Hence String.valueOf(char) seems to be most efficient method, in terms of both memory and speed, for converting char to String.

Sources:

  1. How to convert primitive char to String in Java
  2. How to convert Char to String in Java with Example
like image 33
WarFox Avatar answered Oct 08 '22 05:10

WarFox