2016-11-28 82 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類的一部分。現在沒有這樣的方法。 –

相關問題