【发布时间】:2021-09-29 22:22:25
【问题描述】:
String str = "My name is {0} {1} {2}."
String st = MessageFormat.format(str, "Peter", "", "Maxwell");
所以基本上,这个打印是什么:
我的名字是彼得(空白)麦克斯韦。
假设我的字符串有多个占位符并且我传递了一个空白 参数,在这种情况下,输出会为该空白参数添加空格。
So, in our previous case, the output should be like:
我的名字是 Peter Maxwell。(Peter 和 Maxwell 之间没有多余的空格)
场景 2:
String st = MessageFormat.format(str, "Peter", "Branson", "");
Actual:
**My name is Peter Branson (whitespace).**
Expected:
**My name is Peter Branson.**
我想要实现的是,对于每个未提供的占位符参数 空格不应成为最终输出的一部分。
不胜感激!!
【问题讨论】:
标签: java string messageformat