How do I parse a String
value in Java to a char
type?
I know how to do it to int and double (for example Integer.parseInt("123")), Is there a class for Strings and Chars?
How do I parse a String
value in Java to a char
type?
I know how to do it to int and double (for example Integer.parseInt("123")), Is there a class for Strings and Chars?
The simplest way to convert a
String
to achar
is usingcharAt()
:Here is a full script:
An Essay way :
For Output see this link: Click here
Thanks :-)
If the string is 1 character long, just take that character. If the string is not 1 character long, it cannot be parsed into a character.
org.apache.commons.lang.StringEscapeUtils.(un)EscapeJava methods are probaby what you want
Answer from brainzzy not mine :
https://stackoverflow.com/a/8736043/1130448
You can simply use the
toCharArray()
to convert a string to char array:You can use the .charAt(int) function with Strings to retrieve the char value at any index. If you want to convert the String to a char array, try calling .toCharArray() on the String. If the string is 1 character long, just take that character by calling .charAt(0) (or .First() in C#).