vmg
vmg

Reputation: 10576

How to strike any letter using Unicode in Java

In my Java application I want to output striked letters (like html tag do). Is there any way to do this using Unicode (combine )

Upvotes: 6

Views: 2226

Answers (4)

AJMansfield
AJMansfield

Reputation: 4175

You can use U+0336, the combining long stroke overlay, to accomplish this task.

The official Combining Diacritical Marks unicode chart lists "strikethrough" as an 'informative alias', meaning that this is is the official specified purpose of this character.

0336 ̶◌ COMBINING LONG STROKE OVERLAY
       = strikethrough
       • connects on left and right

For comparison, here is U+0336 compared to the html <strike> tag:

U̶n̶i̶c̶o̶d̶e ̶c̶o̶m̶b̶i̶n̶i̶n̶g̶ ̶l̶o̶n̶g̶ ̶s̶t̶r̶o̶k̶e̶ ̶o̶v̶e̶r̶l̶a̶y̶
Hypertext strike tag

Note that many font rendering engines do not render U+0336 correctly, and when possible one should use markup formatting or another mechanism. Depending on your browser, the above text likely has a large gap in the line around the "m" in combining, and @Alex78191 reports that it renders so low for them that it looks more like an underline than a strikethrough. For this reason, one should still prefer HTML another markup technology over U+0336 for this purpose, given the option.

Upvotes: 5

Michael Borgwardt
Michael Borgwardt

Reputation: 346417

No, this is not possible. While there is the concept of a stroke as diacritic, it's not available as a separate Unicode character, probably because the various letters that use a stroke diacritic do not place it at the same height or even angle. So the result would not resemble strikethrough markup anyway.

To output strikethrough text in Java, you need to use an output format that allows you to use explicit markup. If you have a Swing app, you're in luck as many Swing components support HTML. Otherwise it depends on what presentation technology you're using.

Upvotes: 2

Adrien Clerc
Adrien Clerc

Reputation: 2797

As said before, Unicode doesn't do that, but a lot of Swing components understand basic HTML tags.

JLabel label = new JLabel("<html><s>My stroke</s></html>")

Upvotes: 2

bmargulies
bmargulies

Reputation: 100153

No. Unicode does not define a combining strikeout mark. Unicode's view is that this is the job of markup -- like HTML.

Upvotes: 1

Related Questions