superscript in Java String

后端 未结 7 1690
一向
一向 2020-12-09 18:45

Does Java String supports superscript in a String? If yes then how can I use it, I have searched the web and also the API but not able to figure out how I can use it for my

相关标签:
7条回答
  • 2020-12-09 19:27

    No, a string is just a sequence of UTF-16 code-units. There are unicode codepoints for individual super-script characters in the math code-pages but none that mark a region of a string as super-scripted the way there are for bidi regions.

    If you're trying to display mathematical text with super-scripts using a Graphics context, you should search for Latek or MathML libraries written in Java.

    0 讨论(0)
  • 2020-12-09 19:31

    This can be done in java strings and some other cases also using Unicode Character super script...look at this link.

    0 讨论(0)
  • 2020-12-09 19:33

    Just in case somebody uses theese hand made functions:

    public static String superscript(String str) {
        str = str.replaceAll("0", "⁰");
        str = str.replaceAll("1", "¹");
        str = str.replaceAll("2", "²");
        str = str.replaceAll("3", "³");
        str = str.replaceAll("4", "⁴");
        str = str.replaceAll("5", "⁵");
        str = str.replaceAll("6", "⁶");
        str = str.replaceAll("7", "⁷");
        str = str.replaceAll("8", "⁸");
        str = str.replaceAll("9", "⁹");         
        return str;
    }
    
    public static String subscript(String str) {
        str = str.replaceAll("0", "₀");
        str = str.replaceAll("1", "₁");
        str = str.replaceAll("2", "₂");
        str = str.replaceAll("3", "₃");
        str = str.replaceAll("4", "₄");
        str = str.replaceAll("5", "₅");
        str = str.replaceAll("6", "₆");
        str = str.replaceAll("7", "₇");
        str = str.replaceAll("8", "₈");
        str = str.replaceAll("9", "₉");
        return str;
    }
    

    Note, that there is a little ambiguity about ¹²³, because they are acii symobls 251, 253 and 252 and they are also utf-symbols. I prefer to use acsii because they more probably are supproted by font, but here you should decide what you actually want to use.

    0 讨论(0)
  • 2020-12-09 19:39

    A String does not store formatting information. To use superscript, you will have to fiddle with the Font of the displaying component. Checkout the API on Font.

    0 讨论(0)
  • 2020-12-09 19:39

    You can use html tags in java (User Interface only): Suppose you want to display 210, write this code in your JLabel :

    JLabel lab = JLabel("2<html><sup>10</sup></html>");
    
    0 讨论(0)
  • Check out java.text.AttributedString, which supports subscripts and more. e.g., in your paintComponent() you could go:

       public void paintComponent(Graphics g) {
          super.paintComponent(g);
          AttributedString as = new AttributedString("I love you 104 gazillion");
          as.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 13, 14);
          as.addAttribute(TextAttribute.FOREGROUND, Color.RED, 2, 6);
          g.drawString(as.getIterator(), 20, 20);
       }
    

    Should look like this

    0 讨论(0)
提交回复
热议问题