0

I want to search an element of an array of strings in a string. Like this:

let array:[String] = ["dee", "kamal"]
let str:String = "Hello all how are you, I m here for deepak."

so, I want

str.contain("dee") == true

any possible search in string?

3 Answers 3

2

You can do it in one line by composing a regular expression pattern "(item1|item2|item3)"

let array = ["dee", "kamal"]
let str = "Hello all how are you, I m here for deepak."

let success = str.range(of: "(" + array.joined(separator: "|") + ")", options: .regularExpression) != nil
Sign up to request clarification or add additional context in comments.

Comments

0

You should iterate over the array and for each element, call str.contains.

for word in array {
    if str.contains(word) {
        print("\(word) is part of the string")
    } else {
        print("Word not found")
    }
}

Comments

0

You can do like this:

array.forEach { (item) in
   var isContains:Bool = str.contains(item)
   print(isContains)
}

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.