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

时间:2022-05-21 01:33:08

I have an array containing a number of strings. I have used contains() (see below) to check if a certain string exists in the array however I would like to check if part of a string is in the array?

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

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

searchToSearch = "go"

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

The above code simply checks if a value is present within the array in its entirety however I would like to find "Google, Google and Go"

上面的代码只是检查数组中是否存在整个值,但我想找到“Google,Google和Go”

4 个解决方案

#1


17  

Try like this.

试试这样吧。

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 will contain the list of strings having matched sub strings.

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

In Swift Array struct provides filter method, which will filter a provided array based on filtering text criteria.

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

#2


18  

First of all, you have defined an array with a single string. What you probably want is

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

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

Then you can use contains(array, predicate) and rangeOfString() – optionally with .CaseInsensitiveSearch – to check each string in the array if it contains the search string:

然后你可以使用contains(array,predicate)和rangeOfString() - 可选地使用.CaseInsensitiveSearch - 来检查数组中的每个字符串是否包含搜索字符串:

let itemExists = contains(itemsArray) {
    $0.rangeOfString(searchToSearch, options: .CaseInsensitiveSearch) !=  nil
}

println(itemExists) // true 

Or, if you want an array with the matching items instead of a yes/no result:

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

let matchingTerms = filter(itemsArray) {
    $0.rangeOfString(searchToSearch, options: .CaseInsensitiveSearch) !=  nil
}

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

Update for Swift 3:

Swift 3的更新:

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

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

#3


10  

Try like this.

试试这样吧。

Swift 3.0

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)

Output

产量

["Google", "Goodbye", "Go"]

#4


1  

func filterContentForSearchText(_ searchText: String) {
   filteredString = itemsArray.filter({( item : String) -> Bool in
            return  item.lowercased().contains(searchText.lowercased())
    })
}

#1


17  

Try like this.

试试这样吧。

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 will contain the list of strings having matched sub strings.

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

In Swift Array struct provides filter method, which will filter a provided array based on filtering text criteria.

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

#2


18  

First of all, you have defined an array with a single string. What you probably want is

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

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

Then you can use contains(array, predicate) and rangeOfString() – optionally with .CaseInsensitiveSearch – to check each string in the array if it contains the search string:

然后你可以使用contains(array,predicate)和rangeOfString() - 可选地使用.CaseInsensitiveSearch - 来检查数组中的每个字符串是否包含搜索字符串:

let itemExists = contains(itemsArray) {
    $0.rangeOfString(searchToSearch, options: .CaseInsensitiveSearch) !=  nil
}

println(itemExists) // true 

Or, if you want an array with the matching items instead of a yes/no result:

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

let matchingTerms = filter(itemsArray) {
    $0.rangeOfString(searchToSearch, options: .CaseInsensitiveSearch) !=  nil
}

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

Update for Swift 3:

Swift 3的更新:

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

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

#3


10  

Try like this.

试试这样吧。

Swift 3.0

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)

Output

产量

["Google", "Goodbye", "Go"]

#4


1  

func filterContentForSearchText(_ searchText: String) {
   filteredString = itemsArray.filter({( item : String) -> Bool in
            return  item.lowercased().contains(searchText.lowercased())
    })
}