-2

I am making a trivia game that pulls the questions from https://opentdb.com/ but some of the questions and answers don't come out correct in java for example one of the questions looks like this:

What color is the "Ex" in FedEx Ground?

instaed of:

What color is the "Ex" in FedEx Ground?

any idea how to fix this in Java, thanks.

Thomas Briggs
  • 119
  • 1
  • 11
  • 3
    Can you share the code which you are using to pull the questions and answers ? – stud3nt Nov 30 '19 at 12:14
  • If you're fine with a dependency you could use Apache Commons StringEscapeUtils for unescaping HTML, probably related to this answer: https://stackoverflow.com/questions/1265282/recommended-method-for-escaping-html-in-java – ldz Nov 30 '19 at 12:20
  • I have tried to use a library like that, but they just don't seem to do anything, it doesn't crash or give an error it just leaves them in the string – Thomas Briggs Nov 30 '19 at 12:43
  • That is probably because the example you show is double escaped (`"` -> `"` -> `"`), so you need to double unescape. – Mark Rotteveel Nov 30 '19 at 16:03

1 Answers1

0

The easiest way of doing this would be adding something like this on the end of the code which sets the string.

str = str.replace("&amp;","&").replace("&lt;","<").replace("&gt;",">").replace("&quot;","\"").replace("&apos","'");

This just replaces all the special character codes with the actual character. Also obviously just replace the str variable with whatever the name of your string is.

MorganS42
  • 656
  • 4
  • 21
  • It actually does the reverse: replacing the actual characters by their HTML escape sequence. – JB Nizet Nov 30 '19 at 12:18
  • 1
    Oh, my mistake let me quickly fix that for you! – MorganS42 Nov 30 '19 at 12:18
  • That worked really well, only problem is there are others such as ', what kind of thing could I search to get a list of them? – Thomas Briggs Nov 30 '19 at 12:27
  • I added another replacement and that should be pretty much all the HTML and XML codes. There really isn't any other way. – MorganS42 Nov 30 '19 at 12:32
  • Hey @MorganS42. It's nice that you're helping, but it helps more if we search for existing answers before responding, because we don't want the site to fill up with duplicates and possibly conflicting answers. It helps new people more if they learn this too. Thanks. – Software Engineer Nov 30 '19 at 12:39