Check if a string starts with any of the given prefixes in Java
This post will discuss how to check if a string starts with any of the given prefixes in Java.
1. Using String.startsWith() method
The String.startsWith() method returns true if the string starts with the specified prefix, false otherwise. It can be used as follows:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class Main { private static boolean isWeekday(String day) { return day.startsWith("Mon") || day.startsWith("Tue") || day.startsWith("Wed") || day.startsWith("Thu") || day.startsWith("Fri"); } public static void main(String[] args) { String day = "Friday"; System.out.println(isWeekday(day)? "Weekday": "Weekend"); // Weekday } } |
2. Using Stream.anyMatch() method
Starting with Java 8, you can fold the above expression into the Stream chain, and call the anyMatch() method on the stream which returns true if any elements of the stream match the provided predicate, otherwise false.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.stream.Stream; public class Main { private static boolean isWeekday(String day) { return Stream.of("Mon", "Tue", "Wed", "Thu", "Fri").anyMatch(day::startsWith); } public static void main(String[] args) { String day = "Sunday"; System.out.println(isWeekday(day)? "Weekday": "Weekend"); // Weekend } } |
Before Java 8, you can easily replace the Stream API chain with a loop, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; public class Main { private static boolean isWeekday(String day) { for (String s : Arrays.asList("Mon", "Tue", "Wed", "Thu", "Fri")) { if (day.startsWith(s)) { return true; } } return false; } public static void main(String[] args) { String day = "Friday"; System.out.println(isWeekday(day)? "Weekday": "Weekend"); // Weekend } } |
3. Using String.matches() method
Another alternative is to use regular expressions for this task. The idea is to use the String.matches() method that checks if the string matches the given regular expression.
|
1 2 3 4 5 6 7 8 9 10 11 |
public class Main { private static boolean isWeekday(String day) { return day.matches("(Mon|Tue|Wed|Thu|Fri).*"); } public static void main(String[] args) { String day = "Friday"; System.out.println(isWeekday(day)? "Weekday": "Weekend"); // Weekday } } |
3. Using StringUtils.startsWithAny() method
Apache Commons Lang library has the StringUtils.startsWithAny() method which accepts varargs and checks if the string starts with any of the provided case-sensitive prefixes.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import org.apache.commons.lang3.StringUtils; public class Main { private static boolean isWeekday(String day) { return StringUtils.startsWithAny(day, "Mon", "Tue", "Wed", "Thu", "Fri"); } public static void main(String[] args) { String day = "Sunday"; System.out.println(isWeekday(day)? "Weekday": "Weekend"); // Weekend } } |
That’s all about checking if a string starts with any of the given prefixes 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 :)