Home » Tip Bank » Java
  • Comment on this Article
Language: Java
Expertise: Intermediate
Apr 24, 2020

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Watch

See if a String Contains Given Words in Java

We can use the AnyMatch method to figure out if a string contains any of the given words from an Array. See below for a sample:

List middleEasternCountries = Arrays.asList("egypt", "iran", "turkey");
String sampleString = "Egypt is a famous tourist destination. It contains the Pyramids";

System.out.println(middleEasternCountries.stream().anyMatch(sampleString::contains));
Srinath M S
 
Submit a Tip
Browse "Java" Tips
Browse All Tips
codecademy.com
w3schools.com