0

I'm using

URLDecoder.decode(data,"UTF-8")

to decode my string, e.g. 100%20Natural => 100 Natural

Now the fun part, data can be already encoded, or decoded. So if I have something likes "100% Natural", it will result in Exception.

At the moment I'm laying on Exception to get the correct value, so in case exception happened, I returned the original value.

How I can handle this situation better without laying on the exception to detect if my data needs to be decoded or not? Since throwing exception is also expensive.

Thank you.

Xitrum
  • 7,765
  • 26
  • 90
  • 126
  • 4
    Basically, you can't know what to do. If you have "100%20Natural" as the input, it could mean that the *real* text is "100 Natural" - or it could mean that the *real* text is "100%20Natural" and any previous encoding has already been taken off. It's like not knowing whether a date format is MM/dd/yyyy or dd/MM/yyyy... you'll get things which are ambiguous, plain and simple. – Jon Skeet Oct 13 '16 at 16:08
  • 1
    Possible duplicate of [How to find out if string has already been URL encoded?](http://stackoverflow.com/questions/2295223/how-to-find-out-if-string-has-already-been-url-encoded) – eis Oct 13 '16 at 16:13

0 Answers0