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

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

    Of course, be mindful that your program will only be useful in english speaking countries if you detect dates this way. You might want to consider:

    Set dayNames = Calendar.getInstance()
     .getDisplayNames(Calendar.DAY_OF_WEEK,
          Calendar.SHORT,
          Locale.getDefault())
     .keySet();
    

    From there you can use .startsWith or .matches or whatever other method that others have mentioned above. This way you get the default locale for the jvm. You could always pass in the locale (and maybe default it to the system locale if it's null) as well to be more robust.

提交回复
热议问题