如何检查字符串是否以几个前缀之一开头? [英] How to check if a string starts with one of several prefixes?

查看:31
本文介绍了如何检查字符串是否以几个前缀之一开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 if 语句:

I have the following if statement:

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

我希望它包括 startsWith Mon Tues Weds Thurs Frid​​ay 等.使用字符串时有没有简单的方法?我试过 || 但没用.

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

推荐答案

你的意思是:

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

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

Or you could use regular expression:

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

这篇关于如何检查字符串是否以几个前缀之一开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆