在Java中检查字符属性
好的,我正在编写这个程序,它将丢弃任何不是字母的字符。现在我很难让程序识别哪个是哪个。这是我做的一些代码在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
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);
可以使用正则表达式删除所有不是小写或大写字母的字符:
String userInput2 = question.next();
userInput2 = userInput2.replaceAll("[^a-zA-Z]", "");
System.out.println(userInput2);
遍历字符串并调用每个字符以测试其是否为字母字符。遍历字符串并调用每个字符以测试其是否为字母字符。如果输入可能包含US-ASCII之外的值,unicode类
\p{IsAlphabetic}
可能是正则表达式匹配的更好选择。看,我没有想到这种可能性,因为我认为这是一个英语课程。谢谢你的评论,我刚刚学到了一些新的东西。如果输入可能包含US-ASCII之外的值,unicode类\p{IsAlphabetic}
可能是正则表达式匹配的更好选择。看,我没有想到这种可能性,因为我认为这是一个英语课程。谢谢你的评论,我刚刚从中了解到一些新的东西。