Java:使用unicode上线显示平方根时的字符串长度? [英] Java: length of string when using unicode overline to display square roots?

查看:128
本文介绍了Java:使用unicode上线显示平方根时的字符串长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我创建了一个使用unicode和overline的字符串,因为我试图显示数字的平方根。我需要知道一些格式问题的字符串长度。在unicode中使用组合字符时,查找字符串长度的常用方法似乎失败,如下例所示。任何人都可以帮助我找到随机数在平方根中的第二个字符串的长度,或者如何更好地进行平方根显示的提示?

In Java I create a string that uses unicode and overline because I am trying to display square roots of numbers. I need to know the length of the string for some formatting issues. When using the combining characters in unicode the usual methods for finding string length seem to fail as seen by the following example. Can anyone help me find the length of the second string when random numbers are in the square root, or tips on how to do the square root display better?

    String s = "\u221A"+"12";
    String t = "\u221A"+"1"+"\u0305"+"2"+"\u0305";
    System.out.println(s);
    System.out.println(t);
    System.out.println(s.length());
    System.out.println(t.length());

感谢您的帮助,我在谷歌上找不到任何相关内容。

Thanks for any help, I couldn't find anything on this using google.

推荐答案


查找字符串长度的常用方法似乎失败

the usual methods for finding string length seem to fail

它们不会失败,报告字符串长度为Unicode字符数[*]。如果您需要其他行为,则需要明确定义字符串长度的含义。

They don't fail, the report the string lenght as number of Unicode characters [*]. If you need another behaviour, you need to define clearly what you mean by "string length".

如果您对用于显示目的的字符串长度感兴趣,那么通常您有兴趣计算像素(或其他一些逻辑/物理单位),并且这是显示器的责任图层(首先,如果字体不是等宽字符,则可能有不同宽度的不同字符)。

When you are interested in string lengths for displaying purposes, then usually your are interested in counting pixels (or some other logical/physical unit), and that's responsability of the display layer (to begin with, you might have different widths for different characters, if the font is not monospaced).

但是如果你只想计算数字的数量 graphemes 在特定书写系统中最低限度的独特写作单位 ),此处

But if you're just interested in counting the number of graphemes ("a minimally distinctive unit of writing in the context of a particular writing system"), here's a nice guide with code and examples. Copying-trimming-pasting the relevant code from there, we'd have something like this:

  public static int getGraphemeCount(String text) {
      int graphemeCount = 0;
      BreakIterator graphemeCounter = BreakIterator.getCharacterInstance();
      graphemeCounter.setText(text);
      while (graphemeCounter.next() != BreakIterator.DONE) 
          graphemeCount++;
      return graphemeCount;
  }

请记住:以上使用默认的区域设置。一个更灵活,更健壮的方法是,例如,接收一个显式的 locale 作为参数并调用 BreakIterator.getCharacterInstance(locale) 而不是

Bear in mind: the above uses the default locale. A more flexible and robust method would, eg, receive an explicit locale as argument and invoke BreakIterator.getCharacterInstance(locale) instead

[*]确切地说,正如评论中所指出的, String.length()计数 Java characters ,它们实际上是UTF-16编码的代码单元。这相当于仅当我们在 BMP 中时才计算Unicode字符。

[*] To be precise, as pointed out in comments, String.length() counts Java characters, which are are actually code-units in a UTF-16 encoding. This is equivalent to counting Unicode characters only if we are inside the BMP.

这篇关于Java:使用unicode上线显示平方根时的字符串长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆