作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个字符串,我需要找出字符串中同一位置的匹配字符的数量。例如 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/
我是一名优秀的程序员,十分优秀!