Issue
I have a condition where I have to replace some character(special, non-print-able and other special character) from string as mention below
private static final String NON_ASCII_CHARACTERS = "[^\\x00-\\x7F]";
private static final String ASCII_CONTROL_CHARACTERS = "[\\p{Cntrl}&&[^\r\n\t]]";
private static final String NON_PRINTABLE_CHARACTERS = "\\p{C}";
stringValue.replaceAll(NON_ASCII_CHARACTERS, "").replaceAll(ASCII_CONTROL_CHARACTERS, "")
.replaceAll(NON_PRINTABLE_CHARACTERS, "");
can we refactor above code means we can use single "replaceAll" method and put all conditions inside?
is there any way please advice.
Solution
According to the Pattern
javadocs, it should also be possible to combine the three character class patterns into a single character class:
private static final String NON_ASCII_CHARACTERS = "[^\\x00-\\x7F]";
private static final String ASCII_CONTROL_CHARACTERS = "[\\p{Cntrl}&&[^\r\n\t]]";
private static final String NON_PRINTABLE_CHARACTERS = "\\p{C}";
becomes
private static final String COMBINED =
"[[^\\x00-\\x7F][\\p{Cntrl}&&[^\r\n\t]]\\p{C}]";
or
private static final String COMBINED =
"[" + NON_ASCII_CHARACTERS + ASCII_CONTROL_CHARACTERS
+ NON_PRINTABLE_CHARACTERS + "]";
Note that &&
(intersection) has lower precedence than the implicit union operator so all of the [
and ]
meta-characters in the above are required.
You decide which version you think is clearer. It is a matter of opinion.
Answered By - Stephen C
Answer Checked By - Cary Denson (JavaFixing Admin)