tags:

views:

149

answers:

6

Hello all,

I have just done this in eclipse:

String firstInput = removeSpaces(myIn.readLine());
String first = new String(firstInput.charAt(0));

However, eclipse complains that:

The constructor String(char) is undefined

How do I convert a char to a string then??

Thanks

EDIT

I tried the substring method but it didn't work for some reason but gandalf's way works for me just fine! Very straightforward!

+3  A: 

Easiest way?

String x = 'c'+"";

or of course

String.valueOf('c');
Gandalf
Worked and I understand it. :) It seems a little strange though, maybe because its too easy!
Abs
Concatenating 'c' to "" is less desireable as it uses StringBuilder. String.valueOf is the preferred approach.
Steve Kuo
@Steve - maybe, maybe not. It depends on how smart the compiler is. The JLS allows a Java compiler to rewrite String concatenation expressions to the most efficient equivalent code sequence.
Stephen C
@Stephen I used javac and javap (1.6.0_10) and `'C'+""` results in using StringBuilder. So while the JLS may allow rewriting, the Sun compiler doesn't.
Steve Kuo
+3  A: 
String x = String.valueOf('c');`

That's the most straight forward way.

haffax
+2  A: 
String firstInput = removeSpaces(myIn.readLine());
String first = firstInput.substring(0,1);

This has an advantage that no new storage is allocated.

Alexander Pogrebnyak
-1 - FALSE. New storage **is** allocated to represent the String object created by substring. `String.valueOf(char)` *potentially* doesn't allocate any new space ... if the char is less than 127 and you've already called `valueOf` for that char.
Stephen C
@Stephen. TRUE. At least with Sun SDK. It uses package private String constructor that does offsets magic on the same `char[]` array that `firstInput` is using. Please check SDK source code for that.
Alexander Pogrebnyak
@Alexander. The private String constructor returns a new String object, so NEW STORAGE IS BEING ALLOCATED. On a 32 bit JVM, it is at least 4 words + the object header overhead. Jeez ...
Stephen C
@Stephen. I've added a reply to your answer http://stackoverflow.com/questions/1829421/java-converting-a-char-to-a-string/1829684#1829684. I don't know what JDK you are using, but with regards to Sun 1.6, you cannot go from `char` to `String` without allocating `String` at runtime. `substring` at least does not allocate a new `char[]` array for internal storage. Jeez...
Alexander Pogrebnyak
@Stephen. After you've deleted your wrong answer, have a decency to remove your slandering comments.
Alexander Pogrebnyak
+4  A: 

Instead of...

String first = new String(firstInput.charAt(0));

you could use...

String first = firstInput.substring(0,1);

substring(begin,end) gives you a segment of a string - in this case, 1 character.

Amber
+3  A: 

Why not use substring?

String first = firstInput.substring(0, 1);
karoberts
A: 

You could do this:

String s = Character.toString('k');
Jon
That just calls `Character.valueOf('k')`
Stephen C
No it doesn't - not at all, it calls String.valueOf(char);
Jon