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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9790584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 07:40:47  来源:igfitidea点击:

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

javastringif-statement

提问by FredBones

I have the following if statement:

我有以下 if 语句:

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

I want it to include startsWithMonTuesWedsThursFridayetc. Is there a simple way to this when using strings? I tried ||but it didn't work.

我希望它包括startsWithMonTuesWedsThursFriday等等。使用字符串时有没有简单的方法?我试过了,||但没有用。

采纳答案by hmjd

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).*"))

回答by óscar López

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)) {
    // ...
}

回答by Brendan Long

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)每个项目的整体,因为||仅适用于布尔表达式(真或假)。

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)) {

回答by npinti

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);

回答by Thomas

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
}

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

更新:在某些时候引入 varargs 使调用现在更简单:

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

回答by Matt

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 的默认语言环境。您始终可以传入语言环境(如果它为空,也可以将其默认为系统语言环境),以便更加健壮。

回答by dejvuth

No one mentioned Streamso far, so here it is:

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

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