gpt4 book ai didi

swift - 如何在swift中找到两个字符串之间匹配字符的数量?

转载 作者:行者123 更新时间:2023-11-30 10:45:49 25 4
gpt4 key购买 nike

我有两个字符串,我需要找出字符串中同一位置的匹配字符的数量。例如 1010 和 1100,两个字符在位置 1 和 4 处匹配。有没有办法在 swift 中轻松找到它?

我已将每个字符放入第一个字符串的数组中,并尝试进行处理 -

for chr in string2 {
if chr == array[i] {
count += 1
}
}

这是我的方法,但是否有最简单的解决方案。

最佳答案

您可以使用 zip 来找出同一位置匹配字符的数量。

let x = "1010"
let y = "1100"

let intersection = zip(x, y).filter{ $0 == $1 }
let numberOfMatches = intersection.count

关于swift - 如何在swift中找到两个字符串之间匹配字符的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55781389/

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