'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}", "");
Solution 1:[1]
One option is to use a combination of CharMatchers:
CharMatcher charsToPreserve = CharMatcher.anyOf("\r\n\t");
CharMatcher allButPreserved = charsToPreserve.negate();
CharMatcher controlCharactersToRemove = CharMatcher.JAVA_ISO_CONTROL.and(allButPreserved);
Then use removeFrom as before. I don't know how efficient it is, but it's at least simple.
As noted in edits, JAVA_ISO_CONTROL is now deprecated in Guava; the javaIsoControl() method is preferred.
Solution 2:[2]
This seems to be an option
String s = "\u0001\t\r\n".replaceAll("[\\p{Cntrl}&&[^\r\n\t]]", "");
for (char c : s.toCharArray()) {
System.out.print((int) c + " ");
}
prints 9 13 10 just like you said "except carriage returns, line feeds, and tabs".
Solution 3:[3]
use these
public static String removeNoneAscii(String str){
return str.replaceAll("[^\\x00-\\x7F]", "");
}
public static String removeNonePrintable(String str){ // All Control Char
return str.replaceAll("[\\p{C}]", "");
}
public static String removeOthersControlChar(String str){ // Some Control Char
return str.replaceAll("[\\p{Cntrl}\\p{Cc}\\p{Cf}\\p{Co}\\p{Cn}]", "");
}
public static String removeAllControlChars(String str)
{
return removeNonPrintable(str).replaceAll("[\\r\\n\\t]", "");
}
Solution 4:[4]
In Java regular expression, it is possible to exclude some characters in a character class. Here's a sample program demonstrate something similar:
class test {
public static void main (String argv[]) {
String testStr="abcdefABCDEF";
System.out.println(testStr);
System.out.println(testStr.replaceAll("[\\p{Lower}&&[^cd]]",""));
}
}
It will produce this output:
abcdefABCDEF
cdABCDEF
Solution 5:[5]
I'm using Selenium to test web screens. I use Hamcrest asserts and matchers to search the page source for different strings based on various conditions.
String pageSource = browser.getPageSource();
assertThat("Text not found!", pageSource, containsString(text));
This works just fine using an IE or Firefox driver, but it bombs when using the HtmlUnitDriver. The HtmlUnitDriver formats the page source with tabs, carriage returns, and other control characters. I am using a riff on Nidhish Krishnan's ingenious answer above. If I use Nidish's solution "out of the box," I am left with extra spaces, so I added a private method named filterTextForComparison:
String pageSource = filterTextForComparison(browser.getPageSource());
assertThat("Text not found!", pageSource,
containsString(filterTextForComparison(text)));
And the function:
/**
* Filter out any characters embedded in the text that will interfere with
* comparing Strings.
*
* @param text
* the text to filter.
* @return the text with any extraneous character removed.
*/
private String filterTextForComparison(String text) {
String filteredText = text;
if (filteredText != null) {
filteredText = filteredText.replaceAll("\\p{Cc}", " ").replaceAll("\\s{2,}", " ");
}
return filteredText;
}
First, the method replaces the control characters with a space then it replaces multiple spaces with a single one. I tried doing everything at once with "\p{Cc}+?" but it didn't catch "\t " becoming " ".
Solution 6:[6]
U can use StingUtils from Spring:
String str = "\n\t\t\tsome text\t\t\n";
StringUtils.trimAllWhitespace(str); // some text
Solution 7:[7]
Use StringUtils.deleteWhiteSpace(text) from Apache Commons Lang.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | Raymond Tau |
| Solution 5 | Steve Gelman |
| Solution 6 | ИВÐÐ Ð’ÐЩЕÐКО |
| Solution 7 | Martin Schröder |
