Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6650717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:55:33+00:00 2026-05-26T00:55:33+00:00

In Java I create a string that uses unicode and overline because I am

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:55:33+00:00Added an answer on May 26, 2026 at 12:55 am

    the usual methods for finding string length seem to fail

    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).

    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;
      }
    

    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

    [*] 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a simple Java app that uses JNI to call some
I have a XML schema that I will need to create Java classes for.
I have a Java program that uses reflection to find and invoke the main(String[]
Is it possible in Java to create a static factory method/class that uses an
I'm writing a program that uses java.net.URLDecoder.decode(String value, String encoding) . Apparently, this method
I'm trying to read some information with a REST-Service that uses chunk-encoding. String encodedURL
I’m trying to create an instance of: java.awt.geom.Point2D.Double in ColdFusion. Point2D.Double is a nested
I am trying to create a Key Listener in java however when I try
I have a Java application that uses a C++ DLL via JNI. A few
I have a database table that uses Sql Server 2005's uniqueidentifier column. I know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.