Skip to content Skip to sidebar Skip to footer

Replacing Of Html 5 Codes With Equivalent Characters In Java

I'm trying to replace symbols of HTML 5 using StringEscapeUtils.unescapeHtml4(), but I still have a lot of symbols which haven't been replaced such as ' ',' &'. Wha

Solution 1:

&nbsp and &amp aren't entities.   and & are entities. If your string is really missing the ; on them, that's why they're not being decoded.

I just checked (just to be thorough!), and StringEscapeUtils.unescapeHtml4does correctly decode   and &.

The correct fix is to fix whatever's giving you that string with the incomplete entities in it.

You could workaround it, also turning &nbsp and &amp into \u00A0 and & using String#replace after using StringEscapeUtils.unescapeHtml4:

// Ugly, technically-incorrect workaround (but we do these things sometimes)Stringresult=
    StringEscapeUtils.unescapeHtml4(sourceString)
    .replace("&nbsp", "\u00A0")
    .replace("&amp", "&");

...but it's not correct, because those aren't entities. Best to correct the string.

Post a Comment for "Replacing Of Html 5 Codes With Equivalent Characters In Java"