Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode string in HTML Swift [duplicate]

I have a string and I need to encode it in html. For example ">" becomes "& gt;" In C# I do

string stringEncoded = System.Net.HttpUtility.HtmlEncode(myString);

How can I do the same thing in Swift?

like image 657
jjx Avatar asked Sep 02 '26 11:09

jjx


1 Answers

Might be extremely late by now... At the moment I am doing it like this

    let html = "<pre>something</pre>"
    var result = html.stringByReplacingOccurrencesOfString("&", withString: "&amp;", options: nil, range: nil)
    result = result.stringByReplacingOccurrencesOfString("\"", withString: "&quot;", options: nil, range: nil)
    result = result.stringByReplacingOccurrencesOfString("'", withString: "&#39;", options: nil, range: nil)
    result = result.stringByReplacingOccurrencesOfString("<", withString: "&lt;", options: nil, range: nil)
    result = result.stringByReplacingOccurrencesOfString(">", withString: "&gt;", options: nil, range: nil)
    print(result)
like image 94
rodrigoelp Avatar answered Sep 05 '26 02:09

rodrigoelp