我考虑使用String.replaceAll()
方法来删除字符串中的某些字符。不清楚要删除哪些字符(即想要删除哪些字符),但我会假设任何字符都是有效的(例如[a-zA-Z]
和像$%!
这样的字符等)。
我看到了http://www.java-tips.org/java-se-tips/java.lang/strip-certain-characters-from-a-string.html,但肯定有更好的方法,而不是迭代每个字符...
您对此有什么想法吗?
谢谢
示例:
为了澄清一下,我的字符串长度会变化。我想从它中删除字符,确切的字符在运行时确定,并返回结果字符串。
以上面的段落为例,如果我要剔除“,.
”,我将返回以下字符串:
Just to clarify I will have strings of varying lengths I want to strip characters from it the exact ones to be determined at runtime and return the resulting string
顺便提一下,我知道replaceAll()使用正则表达式,所以如果我想剔除字符"$,.",我需要将它们转义,对吗?