如何检查字符串是否以多个前缀之一开头?

时间:2022-02-21 21:21:55

Ive got the following if statement:

我有以下if语句:

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

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

我希望它包括startsWith'Mon''Tues''Weds''Thrus''Friday'等。使用字符串时有一个简单的方法吗?我试过'||'但它不起作用......

Thanks!

谢谢!

8 个解决方案

#1


137  

Do you mean:

你的意思是:

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

or you could use regular expression:

或者您可以使用正则表达式:

if (newStr4.matches("(Mon|Tues|Wed|Thurs|Fri).*"))

#2


38  

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

除了已经提供的解决方案,您还可以使用Apache Commons Lang库:

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

#3


16  

No one mentioned Stream so far, so here it is:

到目前为止还没有人提到Stream,所以这里是:

if (Stream.of("Mon", "Tues", "Wed", "Thurs", "Fri").anyMatch(s -> newStr4.startsWith(s)))

#4


7  

A simple solution is:

简单的解决方案是:

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

A fancier solution would be:

一个更好的解决方案是:

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

#5


3  

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<String> 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.

从那里你可以使用.startsWith或.matches或其他人上面提到的其他方法。这样您就可以获得jvm的默认语言环境。您总是可以传入区域设置(如果它为null,则可以将其默认为系统区域设置)以及更强大。

#6


1  

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

You need to include the whole str.startsWith(otherStr) for each item, since || only works with boolean expressions (true or false).

因为||,你需要为每个项包含整个str.startsWith(otherStr)仅适用于布尔表达式(true或false)。

There are other options if you have a lot of things to check, like regular expressions, but they tend to be slower and more complicated regular expressions are generally harder to read.

如果你有很多事情需要检查,还有其他选择,比如正则表达式,但它们往往更慢,更复杂的正则表达式通常更难阅读。

An example regular expression for detecting day name abbreviations would be:

用于检测日期名称缩写的示例正则表达式将是:

if(Pattern.matches("Mon|Tues|Wed|Thurs|Fri", stringToCheck)) {

#7


0  

When you say you tried to use OR, how exactly did you try and use it? In your case, what you will need to do would be something like so:

当您说您尝试使用OR时,您是如何尝试使用它的?在你的情况下,你需要做的事情是这样的:

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

#8


0  

Using a List and startsWithAny()

On of the solutions would be putting all the prefixes into a List, then use the StringUtils startsWithAny() method to do the check for you.

解决方案是将所有前缀放入List,然后使用StringUtils startsWithAny()方法为您进行检查。

List<String> prefixesList = Arrays.asList("Mon", "Tues", "Wed", "Thurs", "Fri");
if(StringUtils.startsWithAny(newStr4, prefixesList.toArray(new String[0])))
{
    // Your code goes here ...
}

#1


137  

Do you mean:

你的意思是:

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

or you could use regular expression:

或者您可以使用正则表达式:

if (newStr4.matches("(Mon|Tues|Wed|Thurs|Fri).*"))

#2


38  

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

除了已经提供的解决方案,您还可以使用Apache Commons Lang库:

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

#3


16  

No one mentioned Stream so far, so here it is:

到目前为止还没有人提到Stream,所以这里是:

if (Stream.of("Mon", "Tues", "Wed", "Thurs", "Fri").anyMatch(s -> newStr4.startsWith(s)))

#4


7  

A simple solution is:

简单的解决方案是:

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

A fancier solution would be:

一个更好的解决方案是:

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

#5


3  

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<String> 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.

从那里你可以使用.startsWith或.matches或其他人上面提到的其他方法。这样您就可以获得jvm的默认语言环境。您总是可以传入区域设置(如果它为null,则可以将其默认为系统区域设置)以及更强大。

#6


1  

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

You need to include the whole str.startsWith(otherStr) for each item, since || only works with boolean expressions (true or false).

因为||,你需要为每个项包含整个str.startsWith(otherStr)仅适用于布尔表达式(true或false)。

There are other options if you have a lot of things to check, like regular expressions, but they tend to be slower and more complicated regular expressions are generally harder to read.

如果你有很多事情需要检查,还有其他选择,比如正则表达式,但它们往往更慢,更复杂的正则表达式通常更难阅读。

An example regular expression for detecting day name abbreviations would be:

用于检测日期名称缩写的示例正则表达式将是:

if(Pattern.matches("Mon|Tues|Wed|Thurs|Fri", stringToCheck)) {

#7


0  

When you say you tried to use OR, how exactly did you try and use it? In your case, what you will need to do would be something like so:

当您说您尝试使用OR时,您是如何尝试使用它的?在你的情况下,你需要做的事情是这样的:

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

#8


0  

Using a List and startsWithAny()

On of the solutions would be putting all the prefixes into a List, then use the StringUtils startsWithAny() method to do the check for you.

解决方案是将所有前缀放入List,然后使用StringUtils startsWithAny()方法为您进行检查。

List<String> prefixesList = Arrays.asList("Mon", "Tues", "Wed", "Thurs", "Fri");
if(StringUtils.startsWithAny(newStr4, prefixesList.toArray(new String[0])))
{
    // Your code goes here ...
}