字符串格式启动 缺少格式参数异常由于文本中的两个"%"



我有一个类似:

的文字
DISK_ERROR_MESSAGE = "Server %s is using more than 90% of the disk."

问题是,当我尝试做类似:

之类的事情时
String.format(DISK_ERROR_MESSAGE,host.getName())

java启动错误:

Method threw 'java.util.MissingFormatArgumentException' exception.
java.util.MissingFormatArgumentException: Format specifier '% o'

问题是90%的%。

,但我不知道如何避免它而不将文本分开两个字符串。

要逃脱 %,您需要将其加倍: %%

所以,您需要将字符串更改为

DISK_ERROR_MESSAGE = "Server %s is using more than 90%% of the disk."

最新更新