Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想对 JTextField 运行验证检查以确保它只包含字母。最简单的方法是什么?
您可以使用matches:
matches
str.matches("\\p{Alpha}+")
请注意,\p{Alpha}匹配任何字母(a-z或A-Z),因此\p{Alpha}+匹配任何连续的字母串。
\p{Alpha}
a-z
A-Z
\p{Alpha}+
尝试这个:
public boolean containsOnlyLetters(String s) { for(char c : s.toCharArray()) { if(!Character.isLetter(c)) return false; } return true; }