String.equals()与多个条件(并因此一个动作) [英] String.equals() with multiple conditions (and one action on result)

查看:752
本文介绍了String.equals()与多个条件(并因此一个动作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能做这样的事情在Java中为Android(这是一个伪code)

Is it possible to do something like this in Java for Android (this is a pseudo code)

IF (some_string.equals("john" OR "mary" OR "peter" OR "etc."){
   THEN do something
}

目前,这是通过多次与完成 String.equals()条件|| 其中。

At the moment this is done via multiple String.equals() condition with || among them.

推荐答案

可能性:

  • 使用 String.equals()

if (some_string.equals("john") ||
    some_string.equals("mary") ||
    some_string.equals("peter"))
{
}

  • 使用一个普通的前pression:

  • Use a regular expression:

    if (some_string.matches("john|mary|peter"))
    {
    }
    

  • 存储字符串列表中的集合来进行匹配和搜索的集合:

  • Store a list of strings to be matched against in a Collection and search the collection:

    Set<String> names = new HashSet<String>();
    names.add("john");
    names.add("mary");
    names.add("peter");
    
    if (names.contains(some_string))
    {
    }
    

  • 这篇关于String.equals()与多个条件(并因此一个动作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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