我有一个字符串,我需要在html中对其进行编码。 例如">"成为"& GT;" 在C#我做
string stringEncoded = System.Net.HttpUtility.HtmlEncode(myString);
我如何在Swift中做同样的事情?
答案 0 :(得分:2)
现在可能会非常迟到......目前我正在这样做
let html = "<pre>something</pre>"
var result = html.stringByReplacingOccurrencesOfString("&", withString: "&", options: nil, range: nil)
result = result.stringByReplacingOccurrencesOfString("\"", withString: """, options: nil, range: nil)
result = result.stringByReplacingOccurrencesOfString("'", withString: "'", options: nil, range: nil)
result = result.stringByReplacingOccurrencesOfString("<", withString: "<", options: nil, range: nil)
result = result.stringByReplacingOccurrencesOfString(">", withString: ">", options: nil, range: nil)
print(result)