UnknownFormatConversionException 是由 String.format() 中的符号“%"引起的 [英] UnknownFormatConversionException is caused by symbol '%' in String.format()

查看:56
本文介绍了UnknownFormatConversionException 是由 String.format() 中的符号“%"引起的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

预期:

my string and '%'

但结果是:

java.util.UnknownFormatConversionException: Conversion = '''

为什么?如何正确声明序列 '%' 以便它被 String.format() 忽略?

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

推荐答案

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

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

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

这篇关于UnknownFormatConversionException 是由 String.format() 中的符号“%"引起的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆