删除java中字符串中的特殊字符? [英] Remove special characters in the string in java?
本文介绍了删除java中字符串中的特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何删除字符串中除 - _之外的特殊字符。
现在我使用:
How to remove special characters in the string except "- _". Now I use:
replaceAll("[^\\w\\s]", "")
删除所有特殊字符,但我想保留 - _。谁能告诉我该怎么做?
it remove all special character but i want to keep "- _" . Can anyone tell me how should I do?
推荐答案
使用 replaceAll([^ \\ w \\\\-_],);
我所做的是添加下划线和连字符到正则表达式。我在连字符之前添加了一个 \\
,因为它还用于指定范围: az
表示一个字母之间的所有字母和z。使用 \\
转义它可确保将其视为连字符。
What I did was add the underscore and hyphen to the regular expression. I added a \\
before the hyphen because it also serves for specifying ranges: a-z
means all letters between a and z. Escaping it with \\
makes sure it is treated as an hyphen.
这篇关于删除java中字符串中的特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文