java - 如何在java中将String转换/解析为char?

2022-01-12 00:00:00 string parsing char java

如何在 Java 中将 String 值解析为 char 类型?

How do I parse a String value to a char type, in Java?

我知道如何对 int 和 double 进行处理(例如 Integer.parseInt("123")).有字符串和字符的类吗?

I know how to do it to int and double (for example Integer.parseInt("123")). Is there a class for Strings and Chars?

推荐答案

如果您的字符串只包含一个字符,最简单的将其转换为字符的方法可能是调用 charAt 方法:

If your string contains exactly one character the simplest way to convert it to a character is probably to call the charAt method:

char c = s.charAt(0);

相关文章