I know that removing whitespaces is as easy as String.trim(). But my string contains tab (\t) characters which I would like to keep.
Example:
"teststring\t\t\t ".trimSpaceNotTab() => "teststring\t\t\t"
My current implementation is to use split();
String[] arr = tabbedString.split("\t");
Then joining them somewhere as a string.
I find this implementation slow and ugly.
Is there a better way in Java where I can retain the tabs?
How about
tabbedString.replaceAll("[ \n\x0B\f\r]","")Function used –
String.replaceAll()In case you’d like to also go for tabs and remove them, use a predefined character class
\sPattern Summary