英文:
Replacing "null"s in java.util.formatter output
问题
我想覆盖Java的Formatter行为,以更改其处理空值的方式。目前,每当它遇到null
值时,它会在格式说明符的位置插入字符串"null"
。我试图将这些替换为字符串"NA"
。
仅仅循环遍历参数列表并用"NA"
字符串替换空值是不够的,因为我可能遇到的格式说明符与字符串不兼容。例如,Java允许您编写类似String.format("%.3f", null)
的内容,但不允许String.format("%.3f", "NA")
。
有没有办法做到这一点?
英文:
I'd like to override the behavior of Java's Formatter to change it's behavior for handling nulls. Right now, whenever it encounters a null
value, it inserts the string "null"
in place for the format specifier. I'm trying to instead replace these with the string "NA"
.
It is insufficient for me to simply loop over my argument list and replace the nulls with "NA"
strings, since it may be the case that the format specifier I encounter is incompatible with strings. For example, Java allows you to write something like String.format("%.3f", null)
, but not String.format("%.3f", "NA")
.
Is there any way to do this?
专注分享java语言的经验与见解,让所有开发者获益!
评论