How to add accents to letters in java

71 Views Asked by At

I am trying to combine alphabetical characters with accents in java. For example: Combining the letter "e" (\u0065) with a combing grave accent (\u0300). I have attempted numerous ways in java and believe I should use java.text.Normalizer but cannot figure out how to do it.

I have attempted the following:

String combinedLetter = Normalizer.normalize("\u0065\u0300", Form.NFKD);

And

Char CombinedLetter = (char) (0+0065 + 0+0300));

But neither have worked thus far, any ideas on how to do this? I cannot just use the character for é as I need to do this for every alphabetic character.

1

There are 1 best solutions below

0
Basil Bourque On

You are working too hard. No need to call any normalizer. No need for casting.

Avoid char

The char type has been essentially broken since Java 2, and legacy since Java 5. As a 16-bit value, char is physical incapable of representing most characters.

Code point

To work with individual characters, use Unicode code point integers. You will find code point methods on various classes such as String, StringBuilder, and Character.

Here is an example using code point numbers in hexadecimal within a String literal.

System.out.println( "\u0065\u0300" ) ;

Here is an example using appending code point numbers in decimal via StringBuilder. 65 hex is 101 decimal, and 300 hex is 768 decimal.

String output = new StringBuilder().appendCodePoint( 101 ).appendCodePoint( 768 ).toString() ;
System.out.println( output ) ;

See that code run at Ideone.com.

Any Unicode-savvy text renderer software knows to combine the letter plus diacritic(s) for display. Learn about combining characters is digital typography at Wikipedia.

Character Name Decimal Hex
e LATIN SMALL LETTER E 101 65
` COMBINING GRAVE ACCENT 768 300

For more info, see: