Skip to content
Advertisement

How to check if a string starts with one of several prefixes?

I have the following if statement:

String newStr4 = strr.split("2012")[0];
if (newStr4.startsWith("Mon")) {
    str4.add(newStr4);
}

I want it to include startsWith Mon Tues Weds Thurs Friday etc. Is there a simple way to this when using strings? I tried || but it didn’t work.

Advertisement

Answer

Do you mean this:

if (newStr4.startsWith("Mon") || newStr4.startsWith("Tues") || ...)

Or you could use regular expression:

if (newStr4.matches("(Mon|Tues|Wed|Thurs|Fri).*"))
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement