Let's say I have a text and I want to check if the text contains a couple of words.
let text: String = "The rain in Spain"
let wordsA: [String] = [ "rain", "Spain" ] // should return true when compared with text
let wordsB: [String] = [ "rain", "Italy" ] // should return false when compared with text
What is the shortest, quickest way to check if my text contains ALL of the words?
I know, I can do:
var result: Bool = true
for word in wordsA {
result = result && text.contains(word)
}
But I was wondering if there is a shorter way that involves using predicates. For instance, to check if the string contains ANY of the words I could do:
let result: Bool = wordsA.contains(where: text.contains)
Is there something similar that results only in true if ALL words are found?
allSatisfy