简体   繁体   中英

How can I remove all non-letter characters from a string in Java?

I have:

String str = txtInput.getText(); 
String words[] = str.replaceAll("\\p{P}", "").split("\\s+");   

...but I need it to remove numbers as well.

缩小您的正则表达式,使其仅包含非字母字符(以及空格,因此可以拆分)。

String[] words = str.replaceAll("[^A-za-z ]", "").split("\\s+");
String[] words = str.replaceAll("[0-9]","").split("\\s"});
str.replaceAll("[^A-Za-z ]", "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM