custSex = StringUtils.equals(custSex, "M") ? "1" : StringUtils.equals(custSex, "F") ? "0" : StringUtils.equalsAny(custSex, "0", "1") ? custSex : "";
时间: 2023-07-24 09:16:18 浏览: 41
这行代码是一个条件表达式,根据`custSex`的值来进行判断和赋值操作。如果`custSex`等于"M",则将其赋值为"1";如果`custSex`等于"F",则将其赋值为"0";如果`custSex`等于"0"或"1",则将其保持不变;否则,将其赋值为空字符串""。这段代码使用了`StringUtils`工具类中的一些方法来进行字符串比较和赋值操作。
相关问题
StringUtils.equals
StringUtils.equals是一个Java中的字符串比较工具类方法。它用于比较两个字符串是否相等,返回一个boolean类型的值。这个方法会考虑字符串为null的情况,并且不会抛出NullPointerException异常。
使用方法如下:
StringUtils.equals(CharSequence str1, CharSequence str2)
其中,str1和str2是需要比较的两个字符串。如果它们相等,则返回true;否则返回false。
示例:
String str1 = "abc";
String str2 = "abc";
boolean result = StringUtils.equals(str1, str2);
System.out.println(result); // 输出true
List<DomainInfo> newData = new ArrayList<>(); for(DomainInfo info : data){ String subCode = info.getSystemCode(); if(subCode.contains(";")){ String[] code = subCode.split(";"); if(StringUtils.equals(dto.getSystemCode(), code[0])){ info.setSystemCode(StringUtils.isBlank(code[1]) ? "-" : code[1]); info.setChineseName(StringUtils.isBlank(info.getChineseName()) ? "空值" : info.getChineseName()); newData.add(info); } } } systemCode包含“30;” 解决报错问题
如果代码在检查 systemCode 包含 "30;" 时报错,可能是因为 StringUtils.equals() 方法比较时忽略了字符串中的空格,而传入的 systemCode 字符串中包含了空格。此时可以使用 trim() 方法去掉字符串中的空格后再进行比较,具体代码如下:
```java
List<DomainInfo> newData = new ArrayList<>();
for(DomainInfo info : data){
String subCode = info.getSystemCode();
if(subCode.contains(";")){
String[] code = subCode.split(";");
if(StringUtils.equals(dto.getSystemCode().trim(), code[0].trim())){
info.setSystemCode(StringUtils.isBlank(code[1]) ? "-" : code[1]);
info.setChineseName(StringUtils.isBlank(info.getChineseName()) ? "空值" : info.getChineseName());
newData.add(info);
}
}
}
```
在这里,使用 trim() 方法去掉传入的 systemCode 字符串和 subCode 拆分后的第一个部分中的空格,然后再进行比较。这样就能避免由于空格导致的比较错误。