将学习变成乐趣,一直在路上
每天1990

导航

 
背景:java通过String.format(,)可将文本内容模板化,可用字符串替换前面
例如 String.format("hi %s","张三”),会用张三替换%s的内容。

%在格式化时有特殊意义,那如果我就要用%怎么办?例如html中的width:100%

例子:String.format("<div style=\"width:calc(100% - 265px);\”>%s</div>","内容展示")
会报错:Exception in thread "main" java.util.DuplicateFormatFlagsException: Flags = ' 
 
原因:里面用到了%号,但是却不是做文本替换的。

解决方法:%特殊处理,用%做一次转义,处理结果如下

String.format("<div style=\"width:calc(100%%- 265px);\”>%s</div>","内容展示")

 

posted on 2021-01-25 20:26  每天1990  阅读(13412)  评论(2编辑  收藏  举报