Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

superscript in Java String

Tags:

java

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

like image 280
Hell Boy Avatar asked Nov 09 '11 00:11

Hell Boy


People also ask

How do you add a superscript to a string in Java?

To use superscript, you will have to fiddle with the Font of the displaying component. Checkout the API on Font. Show activity on this post. This can be done in java strings and some other cases also using Unicode Character super script...

How do you subscript a string in Java?

A subscript is an integer value between [ and ] that represents the index of the element you want to get to. The special symbols [ and ] represent the mathematical subscript notation. So instead of x0, x1, and xn-1, Java uses x[0], x[1], and x[n-1].

How do you add a superscript to a string?

You need to use the ^ operator before the superscript. I presume you want to use this character string in a plot or Markdown so here's an example where the label of the X-axis contains a superscript.

How do you represent a superscript?

Use "^" for superscripts: 2^6, e^3, etc. ("**" instead of "^" is also OK.) Use parentheses if either the base or the exponent contains more than one mathematical symbol.


2 Answers

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

like image 152
user949300 Avatar answered Sep 29 '22 18:09

user949300


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.

like image 24
AvrDragon Avatar answered Sep 29 '22 17:09

AvrDragon