在 Java 中,如何将 String 转换为 char 或将 char 转换为 String?

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

有没有办法把一个 char 变成一个 String 或一个带有一个字母的 String 变成一个 char (例如如何将 int 转换为 double 并将 double 转换为 int)?(如果可以,请链接到相关文档).

Is there a way to turn a char into a String or a String with one letter into a char (like how you can turn an int into a double and a double into an int)? (please link to the relevant documentation if you can).

我如何才能找到我在文档中只是模糊地知道的类似内容?

How do I go about finding something like this that I'm only vaguely aware of in the documentation?

推荐答案

char firstLetter = someString.charAt(0);
String oneLetter = String.valueOf(someChar);

您可以通过识别可能涉及的类来查找文档.在这里,候选是 java.lang.Stringjava.lang.Character.

You find the documentation by identifying the classes likely to be involved. Here, candidates are java.lang.String and java.lang.Character.

您应该首先熟悉:

  • java.lang
  • 中的原始包装器
  • java.util中的Java Collection框架
  • Primitive wrappers in java.lang
  • Java Collection framework in java.util

它还有助于通过教程更慢地介绍 API.

It also helps to get introduced to the API more slowly through tutorials.

  • 处理字符串中的字符

相关文章