是的,你不能。看看javadoc。不幸的是,它不适用于 NULL。
尝试使用可选
Optional.ofNullable(value).orElse(0)
或查看如何使用 ChoiceFormat 和 MessageFormat 的示例。
对于更复杂的模式,您可以使用 ChoiceFormat 为单数和复数生成正确的形式:
MessageFormat form = new MessageFormat("磁盘\"{1}\"包含{0}。");
双 [] 文件限制 = {0,1,2};
String[] filepart = {"没有文件","一个文件","{0,number} 个文件"};
ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
form.setFormatByArgumentIndex(0, fileform);
int 文件计数 = 1273;
字符串磁盘名称 = "我的磁盘";
Object[] testArgs = {new Long(fileCount), diskName};
System.out.println(form.format(testArgs));
fileCount 具有不同值的输出:
磁盘“MyDisk”不包含文件。
磁盘“MyDisk”包含一个文件。
磁盘“MyDisk”包含 1,273 个文件。
您可以通过编程方式创建 ChoiceFormat,如上例所示,或使用模式。有关详细信息,请参阅选择格式。
form.applyPattern(
"有{0,choice,0#没有文件|1#是一个文件|1