How to trim no-break space in Java?

You can try this:

string.replaceAll("\\p{Z}","");

From https://www.regular-expressions.info/unicode.html:

\p{Z} or \p{Separator}: any kind of whitespace or invisible separator.


While   is a non breaking space (a space that does not want to be treated as whitespace), you can trim a string while preserving every   within the string with a simple regex:

string.replaceAll("(^\\h*)|(\\h*$)","")
  • \h is a horizontal whitespace character: [ \t\xA0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]

If you are using a pre JDK8 Version, you need to explicitly use the list of chars instead of \h.


U+0160 is not whitespace, so it won't be trimmed. But you can simply replace() that characters with a space, and then call trim(), so you keep the spaces that are 'inside' your string.

string = string.replace('\u00A0',' ').trim()

There are three non-breaking whitespace characters that are excluded from the Character.isWhitespace() method : \u00A0, \u2007 and, \u202F, so you probably want to replace those too.


If you happen to use Apache Commons Lang then you can use strip and add all the characters you want.

final String STRIPPED_CHARS = " \t\u00A0\u1680\u180e\u2000\u200a\u202f\u205f\u3000";

String s = "\u3000 \tThis str contains a non-breaking\u00A0space and a\ttab. ";
s = StringUtils.strip(s, STRIPPED_CHARS);  
System.out.println(s);  // Gives : "This str contains a non-breaking space and a    tab."

Tags:

Java

String