简体   繁体   中英

How to remove invalid characters from a string?

I have no idea how to remove invalid characters from a string in Java. I'm trying to remove all the characters that are not numbers, letters, or ( ) [ ] . How can I do this?

Thanks

String foo = "this is a thing with & in it";
foo = foo.replaceAll("[^A-Za-z0-9()\\[\\]]", "");

Javadocs are your friend. Regular expressions are also your friend.

Edit:

That being siad, this is only for the Latin alphabet; you can adjust accordingly. \\w can be used for a-zA-Z to denote a "word" character if that works for your case though it includes _ .

Using Guava , and almost certainly more efficient (and more readable) than regexes:

CharMatcher desired = CharMatcher.JAVA_DIGIT
  .or(CharMatcher.JAVA_LETTER)
  .or(CharMatcher.anyOf("()[]"))
  .precomputed(); // optional, may improve performance, YMMV
return desired.retainFrom(string);

Try this:

String s = "123abc&^%[]()";
s = s.replaceAll("[^A-Za-z0-9()\\[\\]]", "");
System.out.println(s);

The above will remove characters "&^%" in the sample string, leaving in s only "123abc[]()" .

public static void main(String[] args) {
    String c = "hjdg$h&jk8^i0ssh6+/?:().,+-#";
    System.out.println(c);
    Pattern pt = Pattern.compile("[^a-zA-Z0-9/?:().,'+/-]");
    Matcher match = pt.matcher(c);
    if (!match.matches()) {
        c = c.replaceAll(pt.pattern(), "");
    }
    System.out.println(c);
}

You can remove specials characters from your String/Url or any request parameters you have get from user side

  public static String removeSpecialCharacters(String inputString){
        final String[] metaCharacters = {"../","\\..","\\~","~/","~"};
        String outputString="";
        for (int i = 0 ; i < metaCharacters.length ; i++){
            if(inputString.contains(metaCharacters[i])){
                outputString = inputString.replace(metaCharacters[i],"");
                inputString = outputString;
            }else{
                outputString = inputString;
            }
        }
        return outputString;
   }
["

You can specify the range of characters to keep\/remove based on the order of characters in the ASCII table<\/a> .<\/i>

// Example - remove characters outside of the range of "space to tilde".
// 1) using characters
someString.replaceAll("[^ -~]", "");

// 2) using hex codes for "space" and "tilde"
someString.replaceAll("[^\\u0020-\\u007E]", "");

Use this code:

String s = "Test[]"
s = s.replaceAll("[");
s = s.replaceAll("]");

myString.replaceAll("[^\\w\\[\\]\\(\\)]", "");
replaceAll method takes a regex as first parameter and replaces all matches in string. This regex matches all characters which are not digit, letter or underscore ( \\w ) and braces you need ( \\[\\]\\(\\)] )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM