Check if a string ends with any of the given suffixes in Java
This post will check if a string ends with any of the given suffixes in Java.
There are several ways to check if a string ends with any of the given suffixes in Java. Here are some of them:
1. Using OR operator
We can use the String.endsWith() method, which returns true if the string ends with the specified suffix, and false otherwise. We can use this method with an OR operator to check the string against all possible suffixes.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Main { public static void main(String[] args) { String[] arr = { "AB", "BC", "CD" }; String str = "ABCD"; boolean result = str.endsWith(arr[0]) || str.endsWith(arr[1]) || str.endsWith(arr[2]); System.out.println(result); // true } } |
2. Using Streams API
We can also use Streams API starting from Java 8. The anyMatch() method returns true if any element of the stream matches the predicate, and false otherwise. For example, we can use the following code to check if a string matches one of the given suffixes:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.stream.Stream; class Main { public static void main(String[] args) { String[] arr = { "AB", "BC", "CD" }; String str = "ABCD"; boolean result = Stream.of(arr).anyMatch(str::endsWith); System.out.println(result); // true } } |
This is equivalent to:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Main { public static void main(String[] args) { String[] arr = { "AB", "BC", "CD" }; String str = "ABCD"; boolean match = false; for (String s : arr) { if (str.endsWith(s)) { match = true; break; } } System.out.println(match); // true } } |
3. Using Apache Commons Lang
We can use external libraries such as Apache Commons Lang, which provides the StringUtils.endsWithAny() method. This method takes a string and an array of suffixes as arguments and returns true if the string ends with any of the suffixes, and false otherwise.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.apache.commons.lang3.StringUtils; class Main { public static void main(String[] args) { String[] arr = { "AB", "BC", "CD" }; String str = "ABCD"; boolean result = StringUtils.endsWithAny(str, arr); System.out.println(result); // true } } |
4. Regular expressions
Finally, you can check if a string matches one of the given suffixes using the String.matches() method. It returns true if the string matches a given regular expression, and false otherwise. To specify multiple suffixes in the regex, we can use the | symbol. To indicate the end of the string in the regex, we can use the $ symbol. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Main { public static void main(String[] args) { String[] arr = { "AB", "BC", "CD" }; String str = "ABCD"; String regex = ".*(" + arr[0] + "|" + arr[1] + "|" + arr[2] + ")$"; boolean result = str.matches(regex); System.out.println(result); // true } } |
That’s all about checking if a string ends with any of the given suffixes in Java.
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 :)