-2

I want to remove that characters from a String:

+ - ! ( ) { } [ ] ^ ~ : \

also I want to remove them:

/*
*/
&&
||

I mean that I will not remove & or | I will remove them if the second character follows the first one (/* */ && ||)

How can I do that efficiently and fast at Java?

Example:

a:b+c1|x||c*(?)

will be:

abc1|xc*?
kamaci
  • 72,915
  • 69
  • 228
  • 366

4 Answers4

1

This can be done via a long, but actually very simple regex.

String aString = "a:b+c1|x||c*(?)";
String sanitizedString = aString.replaceAll("[+\\-!(){}\\[\\]^~:\\\\]|/\\*|\\*/|&&|\\|\\|", "");
System.out.println(sanitizedString);
Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
1

I think that the java.lang.String.replaceAll(String regex, String replacement) is all you need:

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String).

Albert Sadowski
  • 642
  • 5
  • 8
0

Thomas wrote on How to remove special characters from a string?:

That depends on what you define as special characters, but try 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".

Another note: the - character needs to be the first or last one on the list, otherwise you'd have to escape it or it would define a range ( e.g. :-, would mean "all characters in the range : to ,).

So, in order to keep consistency and not depend on character positioning, you might want to escape all those characters that have a special meaning in regular expressions (the following list is not complete, so be aware of other characters like (, {, $ etc.):

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]","");

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

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

The regex matches everything that is not a letter in any language and not a separator (whitespace, linebreak etc.). Note that you can't use [\P{L}\P{Z}] (upper case P means not having that property), since that would mean "everything that is not a letter or not whitespace", which almost matches everything, since letters are not whitespace and vice versa.

Community
  • 1
  • 1
Engineer
  • 1,436
  • 3
  • 18
  • 33
0

there is two way to do that :

1)

ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("+");
    arrayList.add("-");
    arrayList.add("||");
    arrayList.add("&&");
    arrayList.add("(");
    arrayList.add(")");
    arrayList.add("{");
    arrayList.add("}");
    arrayList.add("[");
    arrayList.add("]");
    arrayList.add("~");
    arrayList.add("^");
    arrayList.add(":");
    arrayList.add("/");
    arrayList.add("/*");
    arrayList.add("*/");
    String string = "a:b+c1|x||c*(?)";
    for (int i = 0; i < arrayList.size(); i++) {
        if (string.contains(arrayList.get(i)));
            string=string.replace(arrayList.get(i), "");
    }
    System.out.println(string);

2)

    String string = "a:b+c1|x||c*(?)";
    string = string.replaceAll("[+\\-!(){}\\[\\]^~:\\\\]|/\\*|\\*/|&&|\\|\\|", "");
    System.out.println(string);
Mostafa Jamareh
  • 1,389
  • 4
  • 22
  • 54