0

I'm trying to replace some HTML codes in Swift with the appropriate characters. I used a String extension.

extension String {
    mutating func fix_HTML_Codes() {
        let originalString = self
        let newString = originalString.replacingOccurrences(of: "'", with: "\'")
        let newString2 = newString.replacingOccurrences(of: """, with: "\"")

        self = newString2
    }
}

However, instead of replacing my escaped single quote with a single quote, it actually replaces it with \', anyone know why?

Here's an example of what I'm getting:

"On which Beatles album would you find the song \'Eleanor Rigby\'?"

It's including the escape character.

Birdman
  • 1,404
  • 5
  • 22
  • 49
  • if you put it in a UILabel (for instance), do you see the back slash? – Larme Mar 27 '19 at 06:14
  • @Larme shoot I'm not sure let me give it a try. I was just looking in the debugger and assumed it was busted. – Birdman Mar 27 '19 at 06:16
  • Possibly useful: https://stackoverflow.com/questions/25607247/how-do-i-decode-html-entities-in-swift – Martin R Mar 27 '19 at 06:21
  • @Larme You are right, it displays correctly as intended, but in the debugger it shows it as the escape character with slash. Do you know why? – Birdman Mar 27 '19 at 07:01

0 Answers0