0

I'm getting this type of encoded string in response from an API. I dont know what encoding has been used and how to decode it. Need to encode it across ios and android.

  1.5%23%23cups%23%23BISQUICK*+mix%23%23%0A0.333%23%23cup%23%23hot+water%23%23%0A3%23%23pieces%23%23eggs%23%23%0A1%23%23cup%23%23sour+cream%23%23%0A1%23%23cup%23%23Cheddar+cheese%23%23shredded%0A0.5%23%23cup%23%23green+onions%23%23sliced+%28about+4%29%0A0.5%23%23tsp%23%23onion+salt%23%23%0A1%23%23cup%23%23cooked+ham%23%23finely+chopped"
Romit Kumar
  • 2,442
  • 6
  • 21
  • 34

1 Answers1

0

This is a URL string that has been percent encoded (not all characters can be sent in a url, hence the need for encoding).

Use this method to decode:

if let decodedString = yourString.removingPercentEncoding {
    // Do something with decodedString here
}

This answer shows how to add this encoding and decoding as an extension to the String type.

Chris
  • 4,009
  • 3
  • 21
  • 52
  • this is wrong `removingPercentEncoding` returns an optional. Declaration: `var removingPercentEncoding: String? { get }` – Leo Dabus May 27 '18 at 23:25
  • @LeoDabus Thanks. I have updated answer to make variable optional. – Chris May 28 '18 at 07:13
  • 1
    Swift is a type inferred language. Just don't set the type explicitly and use `if let` or `guard` to unwrap the value. `if let decodedString = yourString.removingPercentEncoding {` – Leo Dabus May 28 '18 at 11:33
  • @LeoDabus Thanks again. I omitted the optional binding for the sake of brevity, but you’re right that it’s much better to show how to use the answer in context. – Chris May 28 '18 at 11:53
  • @Romit - this answer will work in Swift on iOS, but I realise your question asked about Android as well. I’m afraid I’m not experienced with Android or Java, but there is likely an equivalent function to encode and decode strings like this. – Chris May 30 '18 at 20:08