java removing special symbol or string of characters designated

String regEx="[\n`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。, 、?]";

// You can add any character you want to replace in the brackets, it is actually a regular expression

String aa = ""; // this is a special character string replaced aa, "" represents a direct removed

 Pattern p = Pattern.compile(regEx);

  Matcher m = p.matcher ( "original string"); // where you wish to replace the string passed in

 String newString = m.replaceAll(aa).trim();

// the replacement string is present in the variable newString

Method Two

If the first too much trouble can directly use the following

String str = "original string";

String newString = str.replaceAll (regEX, aa); // do not want to retain the original string can be directly written "str = str.replaceAll (regEX, aa);"

Guess you like

Origin www.cnblogs.com/hkgov/p/11693252.html