在 Java 中检查字符属性

  • 本文关键字:字符 属性 Java java
  • 更新时间 :
  • 英文 :


好吧,我正在编写这个程序,它将丢弃任何不是字母的字符。 现在我很难让程序识别哪个是哪个。这是我所做的一些代码。

System.out.println("Press enter every time, you type a new word, and press the period button to end it.");
Scanner question = new Scanner(System.in);
System.out.println("Press enter to continue, or tupe something random in");
String userInput = question.next();
while(!userInput.equals(".")){
    String userInput2 = question.next();
    System.out.println(userInput2);
    if(userInput2.equals("Stop")){
        break;
    }
}

您可以使用正则表达式删除所有不是小写或大写字母的字符:

String userInput2 = question.next();
userInput2 = userInput2.replaceAll("[^a-zA-Z]", "");
System.out.println(userInput2);

遍历字符串并为每个字符调用Character.isLetter(char)以测试它是否是字母字符。

最新更新