I have the following code:
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(getLocale());
return numberFormat.format("-123.45").replaceAll("\\s", "");
Since the format returns "-€ 123,45" for my locale, I would expect the code to return "-€123,45", but it returns "-€ 123,45". When inspecting the returned string it says that it has a -96 instead of a space. So obviously instead of inserting a space, format inserts that mysterious -96 character. How can I remove that -96 character?
CodePudding user response:
That is character 0xA0, or \u00A0, or non-breaking space (or NO-BREAK SPACE) in - for example - Windows-1252 and Unicode. This is used to prevent a line break or wrap from occurring after the currency symbol, but before the number.
The regex character class \s
in Java only covers:
A whitespace character: [ \t\n\x0B\f\r]
See javadoc of java.util.regex.Pattern
.
Instead, use \\h
instead of \\s
, as \h
covers:
A horizontal whitespace character: [ \t\xA0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]
Or, only replace \u00a0
.