本文介绍了只保留字母字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我应该在java中使用什么方法来生成
What method should I follow in java to produce
"WordWord"
来自
"Word#$#$% Word 1234"
推荐答案
您可以使用,正则表达式 [^ A-Za-z] +
,如下所示:
You can use String.replaceAll(regex, replacement)
with the regex [^A-Za-z]+
like this:
String newstr = "Word#$#$% Word 1234".replaceAll("[^A-Za-z]+", "");
// newstr will become WordWord
编辑:虽然OP没有提到关于 unicode characters
的任何内容,但由于@Joey发表了评论,并且如果要求保留unicode字符,那么 \\\ \\ P {L} +
正则表达式应该像这样使用:
Although OP hasn't mentioned anything about unicode characters
but since @Joey has made a comment and if at all there a requirement to keep unicode characters then \\P{L}+
regex should be used like this:
String newstr = "Word#$#$% Word λ1234ä, ñ, ж".replaceAll("\\P{L}+", "");
// newstr will become WordWordλäñж
这篇关于只保留字母字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!