2016-11-28 83 views
-3

我有HTML,解析成Swift中的字符串。 HTML来自server.I有如何在Swift中替换HTML字符串中的特定字符?

WebKit的playsinline = “”

内的字符串。

我需要更换这个

"webkit-playsinline="webkit-playsinline" 

我做了两个字符串,用于替换

let string1 = "webkit-playsinline=\"\"" 
let string2 = "webkit-playsinline=\"webkit-playsinline\"" 

但是,我不能做替换,因为没有这样的方法存在。

简而言之,如何在Swift中更改HTML/String。

感谢。

+0

尝试像'let newString = htmlString.replacingOccurrences(of:string1,with:string2)' –

+0

您能否在这里显示HTML字符串。 – vaibhav

+0

replacementOccurrences在String类中不可用。 –

回答

1
let string1 = "webkit-playsinline=\"\"" 
let string2 = "webkit-playsinline=\"webkit-playsinline\"" 

yourHTML = (yourHTML as NSString).stringByReplacingOccurrencesOfString(string1, withString: string2) 

编码快乐!

1

你可以尝试以下方法:

let string1 = "webkit-playsinline=\"\"" 
let string2 = string1.replacingOccurrences(of: "webkit-playsinline=\"\"", with: "webkit-playsinline=\"webkit-playsinline\"") 
+0

replacementOccurrences不是String类的一部分。现在没有这样的方法。 –

相关问题