'superscript in Java String
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
Solution 1:[1]
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.
Solution 2:[2]
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.
Solution 3:[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>");
Solution 4:[4]
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.
Solution 5:[5]
This can be done in java strings and some other cases also using Unicode Character super script...look at this link.
Solution 6:[6]
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:

Solution 7:[7]
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/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Mike Samuel |
| Solution 3 | Pranit Bauva |
| Solution 4 | Mike Adler |
| Solution 5 | |
| Solution 6 | |
| Solution 7 | Chaithu Narayana |
