MessageFormat构造方法因为pattern中有英文单引号导致后面的变量识别为字符串

12 篇文章 0 订阅

在使用MessageFormat进行国际化时发现pattern如果是例如:

Don't support get file which path:{0}

这样包含单引号时,后面的{0}无法正常显示

百度后无果。。。

最后通过阅读MessageFormat的源代码和注释找到了解决方案

applyPattern方法会将读到的第一个英文单引号当做字符串的开始,也就是当成了转义字符,然后如果后面没有单引号出现就会把后面都当做字符串处理。在注释中有给出解决方案:

Within a String, a pair of single quotes can be used to quote any arbitrary characters except single quotes. For example, pattern string "'{0}'" represents string "{0}", not a FormatElement. A single quote itself must be represented by doubled single quotes '' throughout a String. For example, pattern string "'{''}'" is interpreted as a sequence of '{ (start of quoting and a left curly brace), '' (a single quote), and }' (a right curly brace and end of quoting), not '{' and '}' (quoted left and right curly braces): representing string "{'}", not "{}".

当必须使用单引号时需要使用两个单引号来使用,最终还是会显示一个单引号

 

百度不是万能,多看源码

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值