Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 234 Vote(s) - 3.4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if array contains part of a string in Swift?

#1
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?

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"`
Reply

#2
Swift 5

```swift
let itemExist = itemsArray.contains {
$0.range(of: stringToSearch) != nil
}
```
Reply

#3
I had the same problem recently, didn't like most of these answers,
solved it like this:

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())
}
}

This will also correctly trigger searches like "doc", which many of the above answers do not and is best practice.
contains() is more performant than first() != nil
source:

[To see links please register here]

Reply

#4
In Swift 5 with better readability :

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

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

#5
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
}
Reply

#6
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)
Reply

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

#8
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)

Reply

#9
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"]


Reply

#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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through