替换java.util.formatter输出中的“null”

huangapple 未分类评论42阅读模式
英文:

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?

huangapple
  • 本文由 发表于 2020年4月11日 04:07:41
  • 转载请务必保留本文链接:https://java.coder-hub.com/61147910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定