UnknownFormatConversionException is caused by symbol '%' in String.format()

萝らか妹 提交于 2019-12-10 01:16:03

问题


String template = "%s and '%'";
String result = String.format(template, "my string");
System.out.println(result);

Expected:

my string and '%'

But result is:

java.util.UnknownFormatConversionException: Conversion = '''

Why? How to correctly declared the sequence '%' so that it's ignored by String.format()?


回答1:


% is already used by format specifiers so it requires an additional % to display that character:

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


来源:https://stackoverflow.com/questions/16713396/unknownformatconversionexception-is-caused-by-symbol-in-string-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!