Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Unescape HTML special characters of String in Swift
func convertSpecialCharacters(string: String) -> String {
var newString = string
var char_dictionary = [
"&": "&",
"&lt;": "<",
"&gt;": ">",
"&quot;": "\"",
"&apos;": "'"
];
for (escaped_char, unescaped_char) in char_dictionary {
newString = newString.stringByReplacingOccurrencesOfString(escaped_char, withString: unescaped_char, options: NSStringCompareOptions.RegularExpressionSearch, range: nil)
}
return newString
}

blaszku commented Apr 13, 2017

For Swift 3

func convertSpecialCharacters(string: String) -> String {
        var newString = string
        let char_dictionary = [
            "&amp;" : "&",
            "&lt;" : "<",
            "&gt;" : ">",
            "&quot;" : "\"",
            "&apos;" : "'"
        ];
        for (escaped_char, unescaped_char) in char_dictionary {
            newString = newString.replacingOccurrences(of: escaped_char, with: unescaped_char, options: NSString.CompareOptions.literal, range: nil)
        }
        return newString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment