Java:使用带有.endsWith的OR运算符

时间:2017-09-17 12:44:55

标签: java string

我有一堆字符串我想用.endsWith函数将它们分开。我想写这样的东西:

if (textString.endsWith("_xyz"))
   //do this
else if (textString.endsWith("_xyz" || "_pqr" || "_abc")) 
   throw new Error();

是否可以使用.endsWith,如果没有其他最佳方式来达到上述要求?

1 个答案:

答案 0 :(得分:6)

您可以使用regexmatches()来解决问题:

if (textString.matches(".*_(xyz|pqr|abc)$")) {
   // do something
}

详细信息:

  • .*匹配一个或多个字符
  • _(xyz|pqr|abc)$_和三个xyzpqrabc
  • 中的一个结尾