检查数组是否包含 Swift 中的字符串的一部分?

Check if array contains part of a string in Swift?

我有一个包含多个字符串的数组。我已经使用 contains() (见下文)来检查数组中是否存在某个字符串但是我想检查字符串的一部分是否在数组中?

itemsArray = ["Google, Goodbye, Go, Hello"]

searchToSearch = "go"

if contains(itemsArray, stringToSearch) {
    NSLog("Term Exists")
}
else {
    NSLog("Can't find term")
}

上面的代码只是检查一个值是否完整地存在于数组中,但是我想找到 "Google, Google and Go"

这样试试。

let itemsArray = ["Google", "Goodbye", "Go", "Hello"]
let searchToSearch = "go"

let filteredStrings = itemsArray.filter({(item: String) -> Bool in

     var stringMatch = item.lowercaseString.rangeOfString(searchToSearch.lowercaseString)
     return stringMatch != nil ? true : false
})

filteredStrings 将包含具有匹配子字符串的字符串列表。

在Swift Array struct 中提供了过滤方法,该方法将根据过滤文本条件过滤提供的数组。

首先,您定义了一个包含单个字符串的数组。 你可能想要的是

let itemsArray = ["Google", "Goodbye", "Go", "Hello"]

然后你可以使用 contains(array, predicate)rangeOfString() – 可选 .CaseInsensitiveSearch – 检查数组中的每个字符串 如果它包含搜索字符串:

let itemExists = contains(itemsArray) {
    [=11=].rangeOfString(searchToSearch, options: .CaseInsensitiveSearch) !=  nil
}

println(itemExists) // true 

或者,如果您想要一个包含匹配项目的数组而不是 yes/no 结果:

let matchingTerms = filter(itemsArray) {
    [=12=].rangeOfString(searchToSearch, options: .CaseInsensitiveSearch) !=  nil
}

println(matchingTerms) // [Google, Goodbye, Go]

Swift3 的更新:

let itemExists = itemsArray.contains(where: {
    [=13=].range(of: searchToSearch, options: .caseInsensitive) != nil
})
print(itemExists)

let matchingTerms = itemsArray.filter({
    [=13=].range(of: searchToSearch, options: .caseInsensitive) != nil
})
print(matchingTerms)

这样试试。

Swift 3.0

import UIKit

let itemsArray = ["Google", "Goodbye", "Go", "Hello"]

var filterdItemsArray = [String]()


func filterContentForSearchText(searchText: String) {
    filterdItemsArray = itemsArray.filter { item in
        return item.lowercased().contains(searchText.lowercased())
    }
}

filterContentForSearchText(searchText: "Go")
print(filterdItemsArray)

输出

["Google", "Goodbye", "Go"]
func filterContentForSearchText(_ searchText: String) {
   filteredString = itemsArray.filter({( item : String) -> Bool in
            return  item.lowercased().contains(searchText.lowercased())
    })
}

如果您只是检查某个项目是否存在于特定数组中,试试这个:

var a = [1,2,3,4,5]

if a.contains(4) {
    print("Yes, it does contain number 4")
}
else {
    print("No, it doesn't")
}

在Swift 4:

    let itemsArray = ["Google", "Goodbye", "Go", "Hello"]
    let searchString = "Go"
    let filterArray = itemsArray.filter({ { [=10=].range(of: searchString, options: .caseInsensitive) != nil}
    })
    print(filterArray)

标记:- Swift 5,Swift 4

//MARK:- You will find the array when its filter in "filteredStrings" variable you can check it by count if count > 0 its means you have find the results

let itemsArray = ["Google", "Goodbye", "Go", "Hello"]
let searchToSearch = "go"

let filteredStrings = itemsArray.filter({(item: String) -> Bool in

    let stringMatch = item.lowercased().range(of: searchToSearch.lowercased())
    return stringMatch != nil ? true : false
})
print(filteredStrings)


if (filteredStrings as NSArray).count > 0
{
    //Record found
    //MARK:- You can also print the result and can do any kind of work with them
}
else
{
    //Record Not found
}

在 Swift 5 中具有更好的可读性:

let itemsArray = ["Google", "Goodbye", "Go", "Hello"]
let searchString = "Googled"

let result = itemsArray.contains(where: searchString.contains)
print(result) //prints true in the above case.

我最近遇到了同样的问题,不喜欢这些答案中的大部分, 像这样解决:

let keywords = ["doctor", "hospital"] //your array

func keywordsContain(text: String) -> Bool { // text: your search text
    return keywords.contains { (key) -> Bool in
        key.lowercased().contains(text.lowercased())
    }
}

这也将正确触发搜索,如“doc”,上面的许多答案都没有,这是最佳做法。 contains() 比 first() 更高效!= nil 来源:https://www.avanderlee.com/swift/performance-collections/