我有一个字符串,其中我正在检查 3 个条件 //str!=null //或者如果 str 包含速率属性 //如果 str 包含未定义
我已经尝试过这种方式,它有效,但请告诉我这是否是一种有效的通过方式?
package com;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String args[]) {
String str = "{\r\n" +
" \"qty\": \"undefined\",\r\n" +
" \"dswer\": 0,\r\n" +
" \"bag\": {\r\n" +
" \"dm\": \"2\",\r\n" +
" \"rate\": \"---\",\r\n" +
" \"ghy\": \"3013-04-05T00:00:00.000-05:00\",\r\n" +
" \"dee\": \"301304\",\r\n" +
" \"desc\": \"SSAA APR 05 2013 --- CALL\"\r\n" +
" }\r\n" +
"}";
if(str!=null && str.contains("\"rate\": \"---\"") || str.contains("undefined"))
{
System.out.println("Invalid");
}
else
{
System.out.println("Valid");
}
}
}
最佳答案
您需要将 str != null &&
更改为 str == null ||
:
if(str==null || str.contains("\"rate\": \"---\"") || str.contains("undefined"))
否则,当字符串为null
时,该字符串才有效。
关于Java : String Check for null and two attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16276987/