function checkValue(value) {
if (value === null || value === undefined || value === '') {
return '-';
} else {
switch (value) {
case true:
return 'Yes';
break;
case false:
return 'No';
break;
default:
return value;
break;
}
}
}
我知道按字符串切换大小写仅适用于 > 1.7。无论如何,我可以在不使用传统的 if/else
的情况下将这段代码转换为 java 吗?这段代码是用 JavaScript
编写的。
最佳答案
使用枚举代替,因为<1.7 字符串开关不支持
private enum MyEnum {
a, b, c, d;
}
String val; // input
MyEnum mye = MyEnum.valueOf(val);
switch (mye) {
case a:
return something;
case carrot:
return something;
..
}
P.S:伪代码。 IDE 不在手。抱歉。
关于java - 在java中通过字符串切换大小写 < 1.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16168145/