//暴力去除html标签
用i 限制循环次数是为以防万一,按需设置理论不会有问题
思路 : //先搜索> 然后往前搜索 < 成对匹配 然后删除
funcsimpleGetStrFromHtmlStr(str:String) ->String{
varresultStr = str
letnbsp =" "
whileresultStr.contains(nbsp) {
ifletrange = resultStr.range(of: nbsp) {
resultStr.replaceSubrange(range, with:"")
}
}
//先搜索> 然后往前搜索 < 成对匹配 然后删除
ifletrange = resultStr.range(of: resultStr) {
vari =0
whileresultStr.contains("<"), resultStr.contains(">") {
ifletrange1 = resultStr.range(of:">") {
ifletrange0 = resultStr.range(of:"<", options: .backwards, range:Range(uncheckedBounds: (range.lowerBound, range1.lowerBound)), locale:nil) {
letrange2 =Range(uncheckedBounds: (range0.lowerBound, range1.upperBound))
resultStr.replaceSubrange(range2, with:"")
}
}
i +=1
ifi >5{
break
}
}
}
returnresultStr
}
网友评论