0

As part of my application I have written a custom method to extract data from the DB and return it as a string. My string has special characters like the pound sign, which when extracted looks like this:

"MyMobile Blue £54.99 [12 month term]"

I want the £ to be replaced with actual pound symbol. Below is my method:

public String getOfferName(String offerId) {
    log(Level.DEBUG, "Entered getSupOfferName");
    OfferClient client = (OfferClient) ApplicationContext
            .get(OfferClient.class);
    OfferObject offerElement = getOfferElement(client, offerId);
    if (offerElement == null) {
        return "";
    } else {

        return offerElement.getDisplayValue();
    }
}

Can some one help on this?

bmargulies
  • 97,814
  • 39
  • 186
  • 310
shaiksha
  • 993
  • 5
  • 17
  • 35
  • 1
    Why do you think that you don't already have the real Unicode character for £? – bmargulies Mar 25 '13 at 12:23
  • [This](http://stackoverflow.com/questions/994331/java-how-to-decode-html-character-entities-in-java-like-httputility-htmldecode) should help. Link in post seems dead - [here's](http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringEscapeUtils.html#unescapeXml(java.lang.String)) one that works. – Boris the Spider Mar 25 '13 at 12:25
  • @bmargulies - have edited. The OP has the HTML (XML) entity for £. – Boris the Spider Mar 25 '13 at 12:26
  • @bmorris591 post an answer? – bmargulies Mar 25 '13 at 12:27
  • 1
    Is the input XML or HTML? The parser should be responsible for the conversion, not you. – Joni Mar 25 '13 at 12:27

2 Answers2

2

The document contains XML/HTML entities .

You can use the StringEscapeUtils.unescapeXml() method from commons-lang to parse these back to their unicode equivalents.

If this is HTML rather than XML use the other methods as there are differences in the two sets of entities.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
0

I voted for StringEscapeUtils.unescapeXml() solution. Anyway, here's is a custom solution

    String s = "MyMobile Blue £54.99 [12 month term]";
    Pattern p = Pattern.compile("&#(\\d+?);");
    Matcher m = p.matcher(s);
    StringBuffer sb = new StringBuffer();
    while(m.find()) {
        int c = Integer.parseInt(m.group(1));
        m.appendReplacement(sb, "" + (char)c);
    }
    m.appendTail(sb);
    System.out.println(sb);

output

MyMobile Blue £54.99 [12 month term]

note that it does not accept hex entity reference

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275