tags:

views:

152

answers:

4

Is there a way to turn a char into a String or a String with one letter into a char (like how you can turn an int into a double and a double into an int)? (please link to the relevant documentation if you can).

How do I go about finding something like this that I'm only vaguely aware of in the documentation?

+10  A: 
char firstLetter = someString.charAt(0);
String oneLetter = String.valueOf(someChar);

You find the documentation by identifying the classes likely to be involved. Here, candidates are java.lang.String and java.lang.Character.

You should start by familiarizing yourself with:

  • Primitive wrappers in java.lang
  • Java Collection framework in java.util

It also helps to get introduced to the API more slowly through tutorials.

polygenelubricants
see the relevant javadoc for documentation
objects
+3  A: 

String.valueOf('X') will create you a String "X"

"X".charAt(0) will give you the character 'X'

BryanD
+1  A: 

I like to do something like this:

String oneLetter = "" + someChar;
Roman
I strongly dislike it, because it doesn't convey the intent. You don't want to do any addition or concatenation, so `+` is not the right thing to use here.
Joachim Sauer
A: 
String someString = "" + c;
char c = someString.charAt(0);
fastcodejava