gpt4 book ai didi

java - 检查 Java 中的字符属性

转载 作者:行者123 更新时间:2023-12-02 03:57:33 25 4
gpt4 key购买 nike

好吧,我正在编写这个程序,该程序将丢弃所有非字母字符。现在我在尝试让程序识别哪个是哪个时遇到麻烦。这是我所做的一些代码。

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);

关于java - 检查 Java 中的字符属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35283730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com