这是我的字符串:
String field = "first=true, second=true"
first=
和 second=
子字符串,如果是 - 基于它调用其他方法之后的真/假。不过,第一个和第二个子串可能是可选的。这是我到目前为止:void method(String field) {
String[] splittedField = field.split(", ");
for (String substring : splittedField) {
if (substring.contains("first") {
if (substring.contains("true") {
otherMethod("first", "true");
} else if (substring.contains("false") {
otherMethod("first", "false");
}
} else if (substring.contains("second") {
if (substring.contains("true") {
otherMethod("second", "true");
} else if (substring.contains("false") {
otherMethod("second", "false");
}
}
}
}
最佳答案
考虑:
if (substring.contains("first") {
if (substring.contains("true") {
otherMethod("first", "true");
} else if (substring.contains("false") {
otherMethod("first", "false");
}
}
if
可以编码为:if (substring.contains("first") {
String[] valueString = substring.split("=");
otherMethod("first", valueString[1]);
}
https://stackoverflow.com/questions/60564862/