How to convert/parse from String to char in java?

2019-01-04 16:57发布

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?

14条回答
Fickle 薄情
2楼-- · 2019-01-04 17:47

If you want to parse a String to a char, whereas the String object represent more than one character, you just simply use the following expression: char c = (char) Integer.parseInt(s). Where s equals the String you want to parse. Most people forget that char's represent a 16-bit number, and thus can be a part of any numerical expression :)

查看更多
叛逆
3楼-- · 2019-01-04 17:50

you can use this trick :

String s = "p";

char c = s.charAt(0);
查看更多
登录 后发表回答