Check if array contains part of a string in Swift?

◇◆丶佛笑我妖孽 提交于 2019-11-28 05:59:10

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.

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

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:

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:

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

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

Update for 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)

Try like this.

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"]
func filterContentForSearchText(_ searchText: String) {
   filteredString = itemsArray.filter({( item : String) -> Bool in
            return  item.lowercased().contains(searchText.lowercased())
    })
}

If you are just checking if an item exists in a specific array, try this:

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

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

MARK:- 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
}
BIJU C

In Swift 4:

    let itemsArray = ["Google", "Goodbye", "Go", "Hello"]
    let searchString = "Go"
    let filterArray = itemsArray.filter({ { $0.range(of: searchString, options: .caseInsensitive) != nil}
    })
    print(filterArray)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!