I am trying to find if a sentence contains at least 1 word from a different list of words.
I am able to do this like below code, however my concern goes that the "theSentence" is a string that is really huge (it comes from an API response), and I have multiple set of list of words to look for that are also quite big, so I am affraid, that using this type of loops will have a big effect in excecution time potentially leading me to a timeout, I am wondering what will be an alternative of this using?
Boolean wordFound = false;
List<String> words = new List<String>{'Word1', 'word2','....','word N'};
String theSentence = 'Hellow word';
for(String s:words){
if(theSentence.contains(s)){
wordFound = true;
break;
}
}
I am also using the classes Pattern and Matcher, but i still have to use a loop, so I am not sure if this is truly more efficient or not:
List<String> words = new List<String>{'Word1', 'word2','....','word N'};
String separator = '|';
Pattern p = Pattern.compile(String.join(words, separator));
Matcher m = p.matcher(attachment1.Body.toString());
boolean match = false;
while (m.find() == true) {
match = true;
break;
}