我有一个String
例如我有:
String A = "[name:jim,phone:1254]";
通常我的String
是:
String A = "[]";
if(A.contentEquals("[]")){
//do here
}else{
//do here
}
如何验证我的String
。
答案 0 :(得分:2)
String A= A.replace("[", "").replace("]", "");
if(!StringUtils.isEmpty(A)) {
// do if string not empty
}else {
//do if string empty
}
答案 1 :(得分:1)
一种方法可以是
A = A.replace("[", "").replace("]", "");
if(A.equals("")){
//do here if its empty
}else{
//do here if it contains value
}
<强> 修改 强>
正如Jokab在评论中所建议的,这里有更安全的方法来避免NPE(空指针异常)。
A = A.replace("[", "").replace("]", "");
if("".equals(A)){
//do here if its empty
}else{
//do here if it contains value
}
答案 2 :(得分:0)
尝试类似
的内容String A = "[name:jim,phone:1254]";
if(A.matches("\\[\\]")){
//do here if empty
}else{
//do here something
}