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

前端 未结 7 1155
广开言路
广开言路 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:47

    Besides the solutions presented already, you could use the Apache Commons Lang library:

    if(StringUtils.startsWithAny(newStr4, new String[] {"Mon","Tues",...})) {
      //whatever
    }
    

    Update: the introduction of varargs at some point makes the call simpler now:

    StringUtils.startsWithAny(newStr4, "Mon", "Tues",...)
    

提交回复
热议问题