Java: length of string when using unicode overline to display square roots? -
in java create string uses unicode , overline because trying display square roots of numbers. need know length of string formatting issues. when using combining characters in unicode usual methods finding string length seem fail seen following example. can me find length of second string when random numbers in square root, or tips on how 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 help, couldn't find on using google.
the usual methods finding string length seem fail
they don't fail, report string lenght number of unicode characters [*]. if need behaviour, need define mean "string length".
when interested in string lengths displaying purposes, interested in counting pixels (or other logical/physical unit), , that's responsability of display layer (to begin with, might have different widths different characters, if font not monospaced).
but if you're interested in counting number of graphemes ("a minimally distinctive unit of writing in context of particular writing system"), here's nice guide code , examples. copying-trimming-pasting relevant code there, we'd have 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; }
bear in mind: above uses default locale
. more flexible , robust method would, eg, receive explicit locale
argument , invoke breakiterator.getcharacterinstance(locale)
instead
[*] precise, pointed out in comments, string.length()
counts java characters, are code-units in utf-16 encoding. equivalent counting unicode characters if inside bmp.
Comments
Post a Comment