3

I am trying to check wether a grouping separator (char) is a space or not. It's the case for the french locale but my test always prints false.

DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(Locale.forLanguageTag("fr"));
char ch = formatter.getDecimalFormatSymbols().getGroupingSeparator();
System.out.println(ch == ' '); // false
System.out.println(Character.isWhitespace(ch)); // false
Charles Follet
  • 827
  • 1
  • 10
  • 28

3 Answers3

6

The unicode symbol you receive is not a normal whitespace. It's a no-break space. Your char has the integer representation of 160 not 32. To check that you should use:

Character.isSpaceChar(ch);

That method checks if a character is a space according to the unicode standard.

The following method checks if a charactre is a space according to Java specification.

Character.isWhitespace(ch);

A detailed description of the criteria can be found at the documentations.

Andreas Brunnet
  • 722
  • 6
  • 19
  • Oh, nice. How did you find that? – Charles Follet Aug 12 '16 at 08:34
  • @CharlesFollet: Quite simple: System.out.println((int)ch); The 160 confused me as the white space should be at least within ASCII range. So I took a look at a unicode table. Did not know that there are such spaces before either. If the answer helps you please accept it – Andreas Brunnet Aug 12 '16 at 08:36
  • @CharlesFollet Would you accept the answer so other people know that this helped? – Andreas Brunnet Aug 12 '16 at 09:08
0

The grouping character is not space, but 160. This will output true and true

    DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(Locale.forLanguageTag("fr"));
    char ch = formatter.getDecimalFormatSymbols().getGroupingSeparator();
    System.out.println(ch == 160); 
    System.out.println(Character.isSpaceChar(ch));
Guenther
  • 2,035
  • 2
  • 15
  • 20
0

It's non-breaking space

getGroupingSeparator()return non-breaking-space. so you can check it with specific unicode of non-breaking-space.

 public static void main(String[] args) {
    DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(Locale.forLanguageTag("fr"));
    char ch = formatter.getDecimalFormatSymbols().getGroupingSeparator();
    System.out.println(formatter.getDecimalFormatSymbols().getGroupingSeparator() == '\u00A0'); // true
    System.out.println(ch == ' '); // false
    System.out.println(Character.isWhitespace(ch)); // false
}
Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27