[SalesForce] How to check if a string contains an element from a list

What is the best way to check if a string contains an element from a list?

for example:

String email= 'itairu@gmail.com'
List<String> domainNames = split by ; from domains in a custom setting 

for example : (domainNames = [0] - gmail.com, [1] - example.com, [2] - mof.com);

How can I check if email field is contains one of the values in the domainName's List?

I can do it with a Boolean field that will be turned to true if one of field is found for this case –

boolean found = false;
for(String s : domainNames ){
 if(email.contains(s)){
   found =true;  
 }

}

I want to know if there is a better way – in case I will have a huge amount of values to move on in the for loop for this.

Thanks!

Best Answer

A shorter version of your code just checks the domain directly:

Boolean found = domainNames.contains(email.split('@',2)[1]);

There's other ways to do this, too, but this is probably the most straightforward version.