在java中删除字符串中的特殊字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21074485/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:24:52  来源:igfitidea点击:

Remove special characters in the string in java?

javaregexspecial-characters

提问by Minato

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?

它删除了所有特殊字符,但我想保留 "- _" 。谁能告诉我该怎么办?

回答by zmbq

Use replaceAll("[^\\w\\s\\-_]", "");

replaceAll("[^\\w\\s\\-_]", "");

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-zmeans all letters between a and z. Escaping it with \\makes sure it is treated as an hyphen.

我所做的是在正则表达式中添加下划线和连字符。我\\在连字符之前添加了 a ,因为它也用于指定范围:a-z表示 a 和 z 之间的所有字母。转义它\\确保它被视为连字符。

回答by nKn

This might help:

这可能有帮助:

replaceAll("[^a-zA-Z0-9_-]", "");

replaceAll("[^a-zA-Z0-9_-]", "");

回答by rupesh

Use this replaceAll("[\\w\\s\\-\\_\\<.*?>]", "");

使用这个replaceAll("[\\w\\s\\-\\_\\<.*?>]", "");

回答by Cuong

Pattern pt = Pattern.compile("[^a-zA-Z0-9_-]");
    Matcher match = pt.matcher(c);
    while (match.find()) {
        String s = match.group();
        c = c.replaceAll("\" + s, "");
    }

Consider this

考虑这个

回答by Bohemian

I suspect that you need to assignthe result (in case you're not doing that), because replaceAll()returns a newstring, rather than updating the string (String is immutable):

我怀疑您需要分配结果(以防您不这样做),因为replaceAll()返回一个字符串,而不是更新字符串(字符串是不可变的):

str = str.replaceAll("[^\w\s-]", "");

Also note that the regex is quite simple:

另请注意,正则表达式非常简单:

No need to escape the dash -in the character class: When used as a literalin a character class, it must be either first or last (otherwise it indicates a range, like a-zetc).

无需转义-字符类中的破折号:当用作字符类中的文字时,它必须是第一个或最后一个(否则它表示一个范围,例如a-z等)。

No need to mention the underscore at all, because it is alreadylisted: \wincludesthe underscore character!

根本不需要提及下划线,因为它已经列出:\w包括下划线字符!

回答by Kajal Kumari

String str="owl@134_- abc";
String s=str.replaceAll(" [^a-zA-Z_-]+ ", "");
System.out.println(str);

It will replace the special character and white spaces from a given string.

它将替换给定字符串中的特殊字符和空格。

Output will be: owlabc_-

输出将是: owlabc_-

回答by Kaplan

barely 6 years have passed and we have a lambda solution

仅仅 6 年过去了,我们有了 lambda 解决方案

String str = "owl@134_- abc";
str.codePoints().mapToObj( Character::toChars ).filter(
    a -> (a.length == 1 && (Character.isLetterOrDigit( a[0] ) || a[0] == '-' || a[0] == '_')) )
  .collect( StringBuilder::new, StringBuilder::append, StringBuilder::append ).toString(); // owl134_-abc