2

I am getting following characters:

\\u00e2\\u0080\\u0099

from server side and I want to decode this into its correct character which is "`" so please let me know how I can do this in swift.

I don't want to use replace string method as I want a generic solution which should work for all unicode to string conversion.

Thanks,

Adam Richardson
  • 2,518
  • 1
  • 27
  • 31
Rahul gupta
  • 129
  • 8
  • 5
    Possible duplicate of [Working with Unicode code points in Swift](https://stackoverflow.com/questions/31272561/working-with-unicode-code-points-in-swift) – dahiya_boy Aug 29 '18 at 10:43
  • 1
    Are you sure the correct character is the backtick? (\`) `\\u00e2\\u0080\\u0099` is just `â`... – Papershine Aug 29 '18 at 10:52
  • "located in the heart of Wanchai\\u00e2\\u0080\\u0099s Star" this is a string which is coming from the server and how would I know whether this string contains unicode characters or not so let me know which encoding I should apply on this to get the correct string. Yes this is backstick because in android it is showing this. – Rahul gupta Aug 29 '18 at 10:54
  • Possible duplicate of [Swift Encode/decode emojis](https://stackoverflow.com/q/44220794/1187415) – Martin R Aug 29 '18 at 11:41
  • If possible show bytes when dealing with encoding problems. Also, if the server is sending the file via an HTTP response, what does the Content-Type header say, if present? – Tom Blodget Aug 31 '18 at 00:42

1 Answers1

1

If you mean "\\u00e2\\u0080\\u0099" the string of 18 characters, you have to unescape it to convert these sequences to their actual values.

This is a possible way to do it:

let input = "located in the heart of Wanchai\\u00e2\\u0080\\u0099s Star"
let str = String(data: input.data(using: .utf8)!, encoding: String.Encoding.nonLossyASCII)

This gives "located in the heart of Wanchaiâs Star".

As has already been said, "\\u00e2\\u0080\\u0099" represents a "â".

jlasierra
  • 1,096
  • 7
  • 12