Java MessageFormat.format 特殊符号、单引号

为君一笑 提交于 2019-12-14 19:47:52

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

在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'}

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