如何从字符串中删除特殊字符?

我想删除一些特殊字符,比如:

- + ^ . : ,

从一个字符串使用 Java。

573305 次浏览

这取决于您将什么定义为特殊字符,但请尝试 replaceAll(...):

String result = yourString.replaceAll("[-+.^:,]","");

Note that the ^ character must not be the first one in the list, since you'd then either have to escape it or it would mean "any but these characters".

另一个注意事项: -字符必须是列表中的第一个或最后一个字符,否则你必须转义它,否则它将定义一个范围(例如,:-,将意味着“范围 :,中的所有字符)。

因此,为了保持一致性,而不是依赖于字符定位,你可能需要转义所有在正则表达式中有特殊意义的字符(下面的列表不完整,所以要注意其他字符,如 ({$等) :

String result = yourString.replaceAll("[\\-\\+\\.\\^:,]","");


If you want to get rid of all punctuation and symbols, try this regex: \p{P}\p{S} (keep in mind that in Java strings you'd have to escape back slashes: "\\p{P}\\p{S}").

A third way could be something like this, if you can exactly define what should be left in your string:

String  result = yourString.replaceAll("[^\\w\\s]","");

这意味着: 替换所有不是单词字符(无论如何是 a-z,0-9或 _)或空格的内容。

Edit: please note that there are a couple of other patterns that might prove helpful. However, I can't explain them all, so have a look at the reference section of regular-expressions.info.

Here's less restrictive alternative to the "define allowed characters" approach, as suggested by Ray:

String  result = yourString.replaceAll("[^\\p{L}\\p{Z}]","");

正则表达式匹配任何语言中不是字母、不是分隔符(空格、换行符等)的所有内容。请注意,您不能使用 [\P{L}\P{Z}](大写 P 意味着没有这个属性) ,因为这将意味着“所有不是字母或不是空格的东西”,这几乎匹配所有东西,因为字母不是空格,反之亦然。

关于 Unicode 的其他信息

一些 Unicode 字符似乎由于不同的可能的编码方式(作为单个代码点或代码点的组合)而导致问题。详情请参阅 Regular-expressions.info

尝试 String类的 replaceAll()方法。

顺便说一下,这里是方法,返回类型和参数。

public String replaceAll(String regex,
String replacement)

例如:

String str = "Hello +-^ my + - friends ^ ^^-- ^^^ +!";
str = str.replaceAll("[-+^]*", "");

它应该删除所有要删除的{’^’,’+’,’-’}字符!

在 Java 中使用 String.replaceAll()方法。 replaceAll should be good enough for your problem.

正如这里所描述的 Http://developer.android.com/reference/java/util/regex/pattern.html

模式是经过编译的正则表达式。在许多情况下,方便的方法如 String.matchesString.replaceAllString.split会更好,但是如果您需要使用相同的正则表达式执行大量工作,那么一次性编译并重用它可能会更有效率。模式类及其伴侣 Matcher 也提供了比 String 公开的少量功能更多的功能。

public class RegularExpressionTest {


public static void main(String[] args) {
System.out.println("String is = "+getOnlyStrings("!&(*^*(^(+one(&(^()(*)(*&^%$#@!#$%^&*()("));
System.out.println("Number is = "+getOnlyDigits("&(*^*(^(+91-&*9hi-639-0097(&(^("));
}


public static String getOnlyDigits(String s) {
Pattern pattern = Pattern.compile("[^0-9]");
Matcher matcher = pattern.matcher(s);
String number = matcher.replaceAll("");
return number;
}
public static String getOnlyStrings(String s) {
Pattern pattern = Pattern.compile("[^a-z A-Z]");
Matcher matcher = pattern.matcher(s);
String number = matcher.replaceAll("");
return number;
}
}

结果

String is = one
Number is = 9196390097

您可以按以下方式删除单个字符:

String str="+919595354336";


String result = str.replaceAll("\\\\+","");


System.out.println(result);

产出:

919595354336

如果您只想在 java 中执行文字替换,那么可以使用 Pattern.quote(string)将任何字符串转义为文字。

myString.replaceAll(Pattern.quote(matchingStr), replacementStr)

删除特殊字符

String t2 = "!@#$%^&*()-';,./?><+abdd";


t2 = t2.replaceAll("\\W+","");

输出将是: abdd.

这样很好。

这将替换除字母数字之外的所有字符

replaceAll("[^A-Za-z0-9]","");