Search code examples
arraysswiftstringcontains

Check if one element in array is contained in string


let preferredCountryCurrenciesSymbols = ["USD", "EUR", "GBP", "HKD", "JPY", "RUB", "AUD", "CAD"]
let currency = "USDT"

I need code which loop the array and search in the string for .contains

I've tried with this, but it's not finding it

Check if array contains part of a string in Swift?

So basically the function I need is -> Bool have it or don't have it.


Solution

  • The question you linked is the opposite of what you need.

    You can find if any value in the array is contained within currency:

    let preferredCountryCurrenciesSymbols = ["USD", "EUR", "GBP", "HKD", "JPY", "RUB", "AUD", "CAD"]
    let currency = "USDT"
    if preferredCountryCurrenciesSymbols.first(where: { currency.contains($0) }) != nil {
        print("found")
    } else {
        print("not found")
    }