【问题标题】:UnknownFormatConversionException is caused by symbol '%' in String.format()UnknownFormatConversionException 是由 String.format() 中的符号“%”引起的
【发布时间】:2013-05-18 18:45:27
【问题描述】:
String template = "%s and '%'";
String result = String.format(template, "my string");
System.out.println(result);

预期

my string and '%'

但结果是

java.util.UnknownFormatConversionException: Conversion = '''

为什么?如何正确声明序列'%',使其被String.format()忽略?

【问题讨论】:

    标签: java string replace string-formatting


    【解决方案1】:

    % 已被格式说明符使用,因此需要额外的 % 来显示该字符:

    String template = "%s and '%%'";
    

    【讨论】:

    • 感谢@Reimeus。在 stackoverflow 上尝试了许多可能的解决方案后,这个问题让我很生气。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-14
    相关资源
    最近更新 更多