gpt4 book ai didi

swift - 根据数组中的值替换字符串中的多个单词

转载 作者:行者123 更新时间:2023-11-28 09:32:51 25 4
gpt4 key购买 nike

我有一个字符串数组和另一个字符串:

let array = ["one","two","three"]
let string = "one two three four five six seven"

从字符串中删除数组中出现的事件的 Swifty 方法是什么?我尝试了一个 for 循环,但想看看过滤器在这种情况下是否有效?

最佳答案

我相信您想到的那种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

关于swift - 根据数组中的值替换字符串中的多个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55248791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com