Check if a string contains any of substrings from a List in Java
This post will discuss how to check if a string contains any of the substrings from a List.
1. Using String.contains() method
The idea is to iterate over the entire list using an enhanced for loop and call the String.contains() method for each substring. You can terminate the loop on the first match of the substring, or create a utility function that returns true if the specified string contains any of the substrings from the specified list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.List; public class Main { private static boolean hasMatchingSubstring(String str, List<String> substrings) { for (String substring : substrings){ if (str.contains(substring)) { return true; } } return false; } public static void main(String[] args) { String day = "Tuesday"; List<String> weekend = Arrays.asList("Sat", "Sun"); System.out.println(hasMatchingSubstring(day, weekend)? "Yes": "No"); // No } } |
If you need the actual substring that is contained in the string, you can tweak the above utility function like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Arrays; import java.util.List; public class Main { private static String getMatchingSubstring(String str, List<String> substrings) { for (String substring : substrings) { if (str.contains(substring)) { return substring; } } return null; } public static void main(String[] args) { String day = "Sunday"; List<String> weekend = Arrays.asList("Sat", "Sun"); System.out.println(getMatchingSubstring(day, weekend)); // Sun } } |
2. Using Java 8
Starting with Java 8, you can use Stream API for this task. The idea is to call the anyMatch() method on the stream elements, which return true if and only if any element matches with the supplied predicate. To check for a substring, you can use the contains() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; import java.util.List; public class Main { private static boolean hasMatchingSubstring(String str, List<String> substrings) { return substrings.stream().anyMatch(str::contains); } public static void main(String[] args) { String day = "Tuesday"; List<String> weekend = Arrays.asList("Sat", "Sun"); System.out.println(hasMatchingSubstring(day, weekend)? "Yes": "No"); // No } } |
Alternately, you can filter elements with the filter() method to get the actual substring contained in the string, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; import java.util.List; public class Main { private static String getMatchingSubstring(String str, List<String> substrings) { return substrings.stream().filter(str::contains).findAny().orElse(null); } public static void main(String[] args) { String day = "Sunday"; List<String> weekend = Arrays.asList("Sat", "Sun"); System.out.println(getMatchingSubstring(day, weekend)); // Sun } } |
3. Using Apache Commons Lang
Finally, you can leverage the Apache Commons Lang library, which provides the indexOfAny() method in the StringUtils class. It will return the first index of any of a set of given substrings, -1 for no match or null input. However, it accepts varargs, so you have to convert the list to a String array first, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import org.apache.commons.lang3.StringUtils; import java.util.Arrays; import java.util.List; public class Main { private static boolean hasMatchingSubstring(String str, List<String> substrings) { return StringUtils.indexOfAny(str, substrings.toArray(String[]::new)) != -1; } public static void main(String[] args) { String day = "Sunday"; List<String> weekend = Arrays.asList("Sat", "Sun"); System.out.println(hasMatchingSubstring(day, weekend)? "Yes": "No"); // Yes } } |
That’s all about checking if a string contains any of the substrings from a List.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)