7

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!

1
  • Out of curiosity, what "didn't work"? What error message did it give you? Commented Dec 1, 2009 at 22:43

6 Answers 6

15

Easiest way?

String x = 'c'+"";

or of course

String.valueOf('c');
Sign up to request clarification or add additional context in comments.

4 Comments

Worked and I understand it. :) It seems a little strange though, maybe because its too easy!
Concatenating 'c' to "" is less desireable as it uses StringBuilder. String.valueOf is the preferred approach.
@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 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.
4

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.

Comments

3
String x = String.valueOf('c');`

That's the most straight forward way.

Comments

3

Why not use substring?

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

Comments

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

This has an advantage that no new storage is allocated.

5 Comments

-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. 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. 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. I've added a reply to your answer stackoverflow.com/questions/1829421/…. 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...
@Stephen. After you've deleted your wrong answer, have a decency to remove your slandering comments.
1

You could do this:

String s = Character.toString('k');

2 Comments

That just calls Character.valueOf('k')
No it doesn't - not at all, it calls String.valueOf(char);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.