首页javacharJava Data Type - 如何检测是一个字母或空格

Java Data Type - 如何检测是一个字母或空格

我们想知道如何检测是一个字母或空格。
public class StringCharacters {
  public static void main(String[] args) {
    String text = "To be or not to be?";

    int spaces = 0,
    vowels = 0, 
    letters = 0;

    for (int i = 0; i < text.length(); i++) {
      char ch = Character.toLowerCase(text.charAt(i));
      if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
        ++vowels;
      if (Character.isLetter(ch))
        ++letters;
      if (Character.isWhitespace(ch))
        ++spaces;
    }
  }
}