18

I was given a string like

example.com/test?region=us&lang=en&jurisdiction=us

How can I write the java code to decode the encoded characters like

& 

in the string. Is there any existing class/method to decode them?

Thanks.

user256239
  • 17,717
  • 26
  • 77
  • 89

3 Answers3

25

To unescape HTML/XML entities, use Apache Commons Lang StringEscapeUtils or homegrow one.

yegor256
  • 102,010
  • 123
  • 446
  • 597
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 2
    Homegrowing your own is probably a bad idea; use the Apache library. – seansand Dec 22 '11 at 16:38
  • 2
    @sean: even more, homegrowing is always a bad idea. The link is just there to show how it could be done, for the interested or for ones who have a stupid manager who decided a "no 3rd party libraries" restriction. – BalusC Dec 22 '11 at 16:50
  • 1
    I really disagree on the "always a bad idea" part. I have really a problem if someone uses external libraries only to use a few functions. Let alone the version management if (always apache) libraries clashes. – Waverick Feb 18 '16 at 11:04
  • @Waverick: I agree your disagreement. – BalusC Feb 18 '16 at 11:05
2

StringEscapeUtils seems to escape everything

andrebask
  • 774
  • 1
  • 7
  • 18
Jaime Hablutzel
  • 6,117
  • 5
  • 40
  • 57
0

You can also include special characters in XPL. XPL has exactly the same structure as XML, but allows special characters. There is an XML parser with source code available for transformations at http://hll.nu

Roger F. Gay
  • 1,830
  • 2
  • 20
  • 25