`
v5browser
  • 浏览: 1136984 次
社区版块
存档分类
最新评论

Java MessageFormat.format格式化字符串大括号以及单引号问题

 
阅读更多

在MessageFormat.format方法中组装jason数据字符串:{code:"w1",des:"w2"},起止分别有左大括号和右大括号。方法是将单引号把大括号包含起来。如下:

String responseTemplate = "'{'code:\"{0}\",des:\"{1}\"'}'";

System.out.println(MessageFormat.format(responseTemplate, "w1","w2"));

如果格式化字符串中包含单引号,处理方法是用2个单引号进行转义:

String responseTemplate = "'{'code:''{0}'',des:''{1}'''}'";
System.out.println(MessageFormat.format(responseTemplate, "w1","w2"));

处理结果:{code:'w1',des:'w2'}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics