File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes String to char. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String to char." Watch "String to char." New topic

String to char.

Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
How would I convert a string that has the value of "\u0020" (as an example) into a char?

Thanks for the help.
Samuel Behrman
Ranch Hand

Joined: Oct 02, 2009
Posts: 49
You can use the String.charAt(index) method.
If there's just one character in the String, just use charAt(0).

RAQ Report: Web-based Excel-like Java reporting tool.
Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
There isn't. I want to turn the Unicode value into a single char.

So, the string with the value of "\u0020" should be the char of ' ' (space).
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38441
If somebody has already posted the correct answer, please read it.
Atrus Greyor
Ranch Hand

Joined: Mar 07, 2004
Posts: 110
Huh? I don't want to get an individual char from a string.

For example, if I have a JTextField and a JLabel. The user literally types in the JTextField "\u004D", the JLabel should display an 'M'.

However, I figured it out. Just take the last four characters of the string (running under the assumption that it is a valid Unicode number), and put it through Integer.parseInt(string, 16); Cast that result to a char, and done.
I agree. Here's the link:
subject: String to char.