将字符串转换为布尔条件

Anj*_*waj -4 java boolean-expression

说我有一个字符串

String strCondition = something.contains("value") && somethingElse.equals("one");
Run Code Online (Sandbox Code Playgroud)

如何将此String转换为布尔条件,以便我能够在IF语句中使用它?

如果我使用valueOf(),它会评估字符串的内容吗?

重新编辑:我不知道该怎么做.

something.contains("value") && somethingElse.equals("one")从数据库列中获取值 .如果我尝试将其分配给boolean变量,则显示类型不匹配.

Stu*_*ske 6

你没有.

它已经是一个布尔表达式.

something.contains("value")- >返回true或false && somethingElse.equals("one");- >这也返回true或false.

你需要的是:

boolean strCondition = something.contains("value") && somethingElse.equals("one");
if ( strCondition )
Run Code Online (Sandbox Code Playgroud)

要么

if ( something.contains("value") && somethingElse.equals("one"))
Run Code Online (Sandbox Code Playgroud)

编辑:

上述要么返回true,false或抛出一个讨厌的NullPointerException.

为了避免后者,你应该使用:

if ( "one".equals(somethingElse) && (something != null && something.contains("value"))
Run Code Online (Sandbox Code Playgroud)