Skip to content Skip to sidebar Skip to footer

Java How Can I Add An Accented "e" To A String?

With the help of tucuxi from the existing post Java remove HTML from String without regular expressions I have built a method that will parse out any basic HTML tags from a string.

Solution 1:

Java Strings are unicode.

sb += '\u00E9';   # lower case  e + '
sb += '\u00C9';   # upper case  E + '

Solution 2:

You can print out just about any character you like in Java as it uses the Unicode character set.

To find the character you want take a look at the charts here:

http://www.unicode.org/charts/

In the Latin Supplement document you'll see all the unicode numbers for the accented characters. You should see the hex number 00E9 listed for é for example. The numbers for all Latin accented characters are in this document so you should find this pretty useful.

To print use character in a String, just use the Unicode escape sequence of \u followed by the character code like so:

System.out.print("Let's go to the caf\u00E9");

Would produce: "Let's go to the café"

Depending in which version of Java you're using you might find StringBuilders (or StringBuffers if you're multi-threaded) more efficient than using the + operator to concatenate Strings too.

Solution 3:

try this:

if (result.equals("&#x00E9")) {
     sb += char(130);
    }

instead of

if (result.equals("&#x00E9")) {
     sb += "e";
    }

The thing is that you're not adding an accent to the top of the 'e' character, but rather that is a separate character all together. This site lists out the ascii codes for characters.

Solution 4:

For a table of accented in characters in Java take a look at this reference.

To decode the html part, use Apache StringEscapeUtils from Apache commons lang:

import org.apache.commons.lang.StringEscapeUtils; ... String withCharacters = StringEscapeUtils.unescapeHtml(yourString);

See also this Stack Overflow thread: Replace HTML codes with equivalent characters in Java

Post a Comment for "Java How Can I Add An Accented "e" To A String?"