0
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.

2
  • What result do you want? Do you want the index in preferredCountryCurrenciesSymbols for the value found inside currency? Do you just want to know if any value in the array is found inside currency? Show what you actually tried and clearly explain what result you got versus what you want. Commented Jul 14, 2019 at 22:40
  • If any value in the array is found inside currency Commented Jul 14, 2019 at 22:43

4 Answers 4

0

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")
}
Sign up to request clarification or add additional context in comments.

1 Comment

You could use contains(where:) instead of first(where:) != nil. P.S. the downvote wasn't mine and not sure why anyone used one.
0

You can try

let found = preferredCountryCurrenciesSymbols.filter { currency.contains($0)}.count != 0 // can use isEmpty also
print(found)

Comments

0
var res = preferredCountryCurrenciesSymbols.contains { element in
                return element == currency
          }

1 Comment

While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. It will be helpful for future users as well.
0

You can simply use contains(where:) to get that working.

if preferredCountryCurrenciesSymbols.contains(where: { currency.contains($0) }) {
    print("currency found...")
} else {
    print("currency not found...")
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.