This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 38045
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.
Similar Threads
How to sort a String
casting an array of chars to a String??plz help
Why Error Here?
Aggregation and Generalization
changing a String to a char