gpt4 book ai didi

arrays - 在字符串中逐字搜索

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

我有一个 [Patient] 类型的数组,其结构如下:

struct Patients {
var name: String
var lastName: String
var age: Int
var indexed: String
}

“索引”变量等于:

name + " " + lastName + " " + age

我有一个 searchTextField,它通过过滤结果在数组中进行搜索:

let search = self.searchText.stringValue

return arrayOfPatients.filter(){
$0.index.localizedStandardContains(search)
}

如果我在 array.indexed 中有这些行:

Jon McDonalds 18 
Jon Gomez 18
Jon Gomez 37
Tom Gomez 28

如果我搜索“Jon 18”,它不会返回任何结果。或者,如果我搜索“Gomez Jon”,它不会返回任何结果。

我想搜索,例如“18 Jon”并检索所有名为 Jon 的 18 岁患者。

如何改进算法以逐字搜索字符串,忽略空格?

最佳答案

检查这是否适合您。基本上,搜索字符串的每个单词都会与每个患者的索引属性的每个单词进行比较。

如果索引属性包含搜索字符串中的所有单词,则返回 true。

enter image description here

相关代码如下:

let searchStringSeparated = search.components(separatedBy: " ")

let results = arrayOfPatients.filter(){ patient in
!searchStringSeparated.map{ word in
patient.indexed.components(separatedBy: " ").map{
$0.localizedStandardContains(word)
}.contains(true)
}.contains(false)
}

关于arrays - 在字符串中逐字搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48272274/

25 4 0
文章推荐: javascript - 为什么 在多次渲染时不显示复选框?