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

前端 未结 7 1158
广开言路
广开言路 2020-11-27 06:04

I have the following if statement:

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

I

7条回答
  •  庸人自扰
    2020-11-27 06:27

    A simple solution is:

    if (newStr4.startsWith("Mon") || newStr4.startsWith("Tue") || newStr4.startsWith("Wed"))
    // ... you get the idea ...
    

    A fancier solution would be:

    List days = Arrays.asList("SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT");
    String day = newStr4.substring(0, 3).toUpperCase();
    if (days.contains(day)) {
        // ...
    }
    

提交回复
热议问题