16

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 purpose

Although this will be printed on the webpage, I cannot use the html tag here, any suggestions please

3
  • 1
    A Java String is just a collection of characters, i.e. plain text... Nov 9, 2011 at 0:04
  • 1
    ... Well. If you want superscripts in a browser, you're going to have to use CSS or HTML. A string is just that-a string, it doesn't have any intrinsic formatting. Nov 9, 2011 at 0:05
  • 1
    I don't know why everyone is talking about graphics components and fonts. OP, please clarify your question: where and how will this string be displayed? Nov 9, 2011 at 0:32

8 Answers 8

21

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

1
18

Just in case somebody uses these handmade 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 acsii symbols 251, 253 and 252 and they are also utf-symbols. I prefer to use acsii because they more probably are supported by font, but here you should decide what you actually want to use.

7
  • There's no such thing as a UTF symbol, and codepoints 251-253 in unicode are 'ûüý'. Also, your subscript '5' is actually a superscript. Oct 11, 2012 at 16:26
  • 1. symbol supported in UTF-8 is an utf-symbol 2. thanks, corrected
    – AvrDragon
    Oct 11, 2012 at 16:35
  • 2
    UTF-8 and UTF-16 and UTF-32 are not symbol sets. They are mappings from byte strings to strings of integers of varying sizes. Unicode and ASCII are mappings from integers to symbols so there are ASCII symbols and unicode symbols, but not UTF-* symbols, only UTF-?? encodings of unicode/ASCII symbols. As you can see, the only numeric symbols in extended ASCII are regular digits and fractions. No superscripts or subscripts. Oct 11, 2012 at 20:36
  • @Mike Samuel 1. thank you for explaination about utf mappings and symbol mappings. 2. But you are actually not correca about ¹²³ in extended ASCII(Latin-1). You can find them following your link. DEC codes 178=², 179=³ and 185=¹. Now i am not sure about ambiguity and wondering what do the symbols 0x2071, 0x2072 and 0x2073 are supposed to mean in unicode table tamasoft.co.jp/en/general-info/unicode.html. The symbols ⁴-⁹ have codes 0x2074-0x2079, ⁰=0x2070, but 0x2071-3 are shown as ⁱ⁲⁳ in my browser. May be they are a latin superscripted symbols, i am not sure.
    – AvrDragon
    Oct 12, 2012 at 10:08
  • I see the 178,179,185 entries in the table now. Apologies, I must have missed them in my earlier scan. unicode.org/charts/PDF/U2070.pdf says "2071 i SUPERSCRIPT SMALL LATIN I - functions as a modifier letter", and "2072 reserved" and "2073 reserved" so it looks like there are no superscript 1,2,or 3 in that code page. Oct 12, 2012 at 13:52
5

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.

3

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>");
2

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.

1

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

1

Here is the example how to superscript the string "JavaTM" in Java.

in as1.addAttribute method '4' is the beginIndex Index of the first character and '6' is the endIndex Index of the character for superscript.

Example:

public class TextAttributesSuperscript extends JPanel {
public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    AttributedString as1 = new AttributedString("JavaTM");
    as1.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 4, 6);
    g2d.drawString(as1.getIterator(), 15, 60);
}

public static void main(String[] args) {
    JFrame frame = new JFrame("Java Superscript Example");
    frame.add(new TextAttributesSuperscript());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setSize(320, 190);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

}

Output of this program:

enter image description here

1

This can be achieved using unicode characters as seen here:

   System.out.println("cm\u00b2");
   System.out.println("meter/second\u00b2");
   System.out.println("meter/second\u00b3");

This results in the below output:

cm²
meter/second²
meter/second³

More unicode characters can be found here - https://unicode-table.com/en/

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.