Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove control characters from java string?

I have a string coming from UI that may contains control characters, and I want to remove all control characters except carriage returns, line feeds, and tabs.

Right now I can find two way to remove all control characters:

1- using guava:

return CharMatcher.JAVA_ISO_CONTROL.removeFrom(string); 

2- using regex:

return string.replaceAll("\\p{Cntrl}", ""); 
like image 539
Mahmoud Saleh Avatar asked Dec 25 '12 07:12

Mahmoud Saleh


People also ask

What is control character in Java?

Control characters are characters that don't represent printable character yet rather serves to start particular action. Control characters are utilized to execute any action, in contrast, to print printable character on display.


1 Answers

You can do something like this if you want to delete all characters in other or control uni-code category

System.out.println(     "a\u0000b\u0007c\u008fd".replaceAll("\\p{Cc}", "") ); // abcd 

Note : This actually removes (among others) '\u008f' Unicode character from the string, not the escaped form "%8F" string.

Courtesy : polygenelubricants ( Replace Unicode Control Characters )

like image 146
Nidhish Krishnan Avatar answered Sep 28 '22 17:09

Nidhish Krishnan