我有一个字符串数组和另一个字符串:
let array = ["one","two","three"]
let string = "one two three four five six seven"
Run Code Online (Sandbox Code Playgroud)
从字符串中删除数组中出现的事件的 Swifty 方法是什么?我尝试了 for 循环,但想看看 filter 在这种情况下是否有效?
我相信filter你想到的表达方式是这样的:
let finalString = string
.split(separator: " ") // Split them
.lazy // Maybe it's very long, and we don't want intermediates
.map(String.init) // Convert to the same type as array
.filter { !array.contains($0) } // Filter
.joined(separator: " ") // Put them back together
Run Code Online (Sandbox Code Playgroud)