简体   繁体   English

检查分组分隔符是否为空格

[英]Check if a grouping separator is a space

I am trying to check wether a grouping separator ( char ) is a space or not. 我试图检查分组分隔符( char )是否是空格。 It's the case for the french locale but my test always prints false . 这是法国语言环境的情况,但我的测试总是打印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

The unicode symbol you receive is not a normal whitespace . 您收到的unicode符号不是普通的空格 It's a no-break space . 这是一个不间断的空间 Your char has the integer representation of 160 not 32 . 您的char的整数表示形式为160而不是32 To check that you should use: 要检查你应该使用:

Character.isSpaceChar(ch);

That method checks if a character is a space according to the unicode standard. 该方法根据unicode标准检查字符是否为空格。

The following method checks if a charactre is a space according to Java specification. 以下方法根据Java规范检查charactre是否为空格。

Character.isWhitespace(ch);

A detailed description of the criteria can be found at the documentations. 可以在文件中找到标准的详细描述。

The grouping character is not space, but 160. This will output true and true 分组字符不是空格,而是160.这将输出true和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));

It's non-breaking space 这是一个不间断的空间

getGroupingSeparator() return non-breaking-space. getGroupingSeparator()返回非破坏空间。 so you can check it with specific unicode of non-breaking-space. 所以你可以用特定的非破坏空间unicode来检查它。

 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM