Java 如何从我的字符串中删除一些字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4430112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I remove some characters from my String
提问by code511788465541441
i want to remove all of the following chars from my string
我想从我的字符串中删除以下所有字符
">[],-"
">[],-"
at the moment im doing this. but there must be a more efficient way
目前我正在这样做。但必须有更有效的方法
newString = myString.replace(">","").replace("[","").replace("]","")....
采纳答案by Karl Knechtel
Use a regex that describes all the characters you want to replace, with the method that replaces everything matching the regex:
使用描述要替换的所有字符的正则表达式,以及替换与正则表达式匹配的所有字符的方法:
newString = myString.replaceAll("[<>\[\],-]", "");
(edited: I don't think <>
are supposed to be escaped, actually. And I forgot to double up the backslashes since they'll be interpreted twice: once by the Java compiler, and again by the regular expression engine.)
(编辑:我认为<>
实际上不应该被转义。而且我忘记将反斜杠加倍,因为它们将被解释两次:一次由 Java 编译器解释,另一次由正则表达式引擎解释。)
回答by Darron
newString = myString.replaceAll("[>\[\],-]", "");
The backslashes are to escape the '[' because the first argument is actually a regular expression.
反斜杠是为了转义“[”,因为第一个参数实际上是一个正则表达式。
回答by Jonathan Wood
You could loop through each character, appending them one by one to a StringBuilder() unless they match one of the characters you are removing.
您可以遍历每个字符,将它们一个一个地附加到 StringBuilder() ,除非它们与您要删除的字符之一匹配。
But I'm not sure that'd be a whole lot faster.
但我不确定那会快很多。
回答by darioo
newString = myString.replaceAll("[>\[\],-]", "");
回答by Kurt Kaylor
Using the replace / replaceAll methods causes a new Pattern to be compiled each time. So if you are doing this multiple times I would strongly suggest either looping through the characters or creating a single regular expression to use repeatedly.
使用 replace / replaceAll 方法会导致每次编译一个新的 Pattern。因此,如果您多次执行此操作,我强烈建议您循环遍历字符或创建单个正则表达式以重复使用。
回答by Andreas Dolk
More efficient performace-wise: yes. You can't "delete" chars from a String so you have t create a new one anyway. Consider a switch/case based solution:
更高效的性能:是的。您不能从字符串中“删除”字符,因此无论如何您都不必创建一个新字符。考虑基于 switch/case 的解决方案:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
switch(s.getCharAt(i)) {
case '>':
case '[':
case ']':
case ',':
case '-': break;
default: sb.append(s.getCharAt(i));
}
}
s = sb.toString();
回答by codaddict
You can use the replaceAll
method of the String
class.
您可以使用类的replaceAll
方法String
。
You can form a character class consisting of the characters you want to delete. And the replacement string will be empty string ""
.
您可以形成一个由您要删除的字符组成的字符类。并且替换字符串将为空字符串""
。
But the characters you want to delete which you'll be putting in the character class might be regex meta-characters and hence need to be escaped. You can manually escape them as many answers show, alternatively you can use the Pattern.quote()
method.
但是您要删除的字符可能是正则表达式元字符,因此需要对其进行转义。您可以像许多答案显示的那样手动转义它们,或者您可以使用该Pattern.quote()
方法。
String charToDel = ">[],-";
String pat = "[" + Pattern.quote(charToDel) + "]";
String str = "a>b[c]d,e-f";
str = str.replaceAll(pat,"");