String.format启动MissingFormatArgumentException,因为文本中有两个“%”[重复]

问题描述 投票:-2回答:1

这个问题在这里已有答案:

我有一个文字像:

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%的百分比。

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

java string
1个回答
-1
投票

要逃离%,你需要加倍:%%

因此,您需要将字符串更改为

DISK_ERROR_MESSAGE = "Server %s is using more than 90%% of the disk."
© www.soinside.com 2019 - 2024. All rights reserved.