0

I'm getting string like this from my api \\u{1F60A}, But I'm not able to display emoji with this. I need this type format \u{1F60A} to show emoji on the UILabel. So with 2 "\\" I'm not able to show emoji in the screen and need to remove 1 "\" from string. I've tried below codes to remove "\" and have only 1 "\". But the result was either \\u{1F60A}(with \ slashes) or u{1F60A} (with no slashes).

let text  = "\\u{1F60A}"

text = text.replacingOccurrences(of: "\\", with: "", options: .literal, range: nil)  // producing the result "u{1F60A}"

text = text.replacingOccurrences(of: "\\\\", with: "\\", options: .literal, range: nil) // producing the result "\\u{1F60A}"

I'm not able to display the emoji in the text. I'm using xcode 10.2 and swift 4.2

Any help appreciated. TIA

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
Arun K
  • 810
  • 2
  • 12
  • 33
  • The string literal `"\\u{1F60A}"` contains the escape sequence `"\\"`, which represents one backslash character. – molbdnilo Jul 05 '19 at 08:47
  • Yes but it not working. If I use "\u{1F60A}" then only emoji is displayed but if I use "\\u{1F60A}" then emoji is not displayed – Arun K Jul 05 '19 at 08:50
  • That is the way it's supposed to work. `"\u{1F60A}"` is one Unicode character long, `"\\u{1F60A}"` is nine characters long. – molbdnilo Jul 05 '19 at 08:51
  • Possibly helpful: https://stackoverflow.com/q/37938823/1187415. – Martin R Jul 05 '19 at 08:53
  • The problem is what I'm getting as string from api is "\\u{1F60A}" but I need to convert it into "\u{1F60A}" so that it should show emoji and I'm not able to convert it. – Arun K Jul 05 '19 at 08:54
  • If the string is those ten characters, you need to parse it yourself and convert to the proper number. No amount of removing backslashes will help. – molbdnilo Jul 05 '19 at 08:58
  • Thanks for the suggestion, but not able to parse that's why I've asked question regarding this. – Arun K Jul 05 '19 at 09:00
  • You could use a regular expression to extract the bit between then braces, then convert that to an integer, then convert that to the corresponding unicode character. – molbdnilo Jul 05 '19 at 09:26

0 Answers0