ios 替换字符串数组的出现次数

kxxlusnw  于 11个月前  发布在  iOS
关注(0)|答案(1)|浏览(108)

这是我的数组

["<h2>Some text, and some info.</h2>", "<h2>Another text</h2>"]

字符串
我得让它看起来像这样

<h2>Some text, and some info.</h2><h2>Another text</h2>


我需要删除["", ""]
尝试extension[String]数组上调用replcaingOccurances

extension Array where Element == String {
    
   func replacingOccurrences(of target: String, with replacement: String, options: NSString.CompareOptions) -> Self
     {
            return self.map( { $0.replacingOccurrences(of: target, with: replacement, options: options) } )
        }
    }


背景:我正在Core Data中的array中保存html数据
尝试使用扩展名不会过滤任何事件

let result = ResultCoreDataManager.sharedInstance.getResults()
let filtered = result?.replacingOccurrences(of: "<[\"\"]\",\"\", \"", with: "", options: .regularExpression)


帮帮忙?

svmlkihl

svmlkihl1#

它是一个有两个元素的字符串数组,那么为什么不直接使用joined呢?

let htmls = ["<h2>Some text, and some info.</h2>", "<h2>Another text</h2>"]
let output = htmls.joined()
print(output)
//<h2>Some text, and some info.</h2><h2>Another text</h2>

字符串

相关问题