我有一堆字符串我想用.endsWith
函数将它们分开。我想写这样的东西:
if (textString.endsWith("_xyz"))
//do this
else if (textString.endsWith("_xyz" || "_pqr" || "_abc"))
throw new Error();
是否可以使用.endsWith
,如果没有其他最佳方式来达到上述要求?
答案 0 :(得分:6)
您可以使用regex和matches()
来解决问题:
if (textString.matches(".*_(xyz|pqr|abc)$")) {
// do something
}
详细信息:
.*
匹配一个或多个字符_(xyz|pqr|abc)$
以_
和三个xyz
或pqr
或abc