我们知道java中可以用MessageFormat.format来格式化字符串。这个方法在我们的实际开发中经常用到,有点类似模板,这样我们就不需要用很恶心的拼接字符串了。如下面
String s1="my blogWebSite is {0} and sinaWeiBo is {1} "; String s2=MessageFormat.format(s1,"http://www.3lai8.com","http://weibo.com/javaee6"); System.out.println(s2);
结果打印
my blogWebSite is http://www.3lai8.com and sinaWeiBo is http://weibo.com/javaee6
继续看
String s1="my blogWebSite is ‘{0}‘ and sinaWeiBo is {1} "; String s2=MessageFormat.format(s1,"http://www.3lai8.com","http://weibo.com/javaee6"); System.out.println(s2);
结果打印
my blogWebSite is {0} and sinaWeiBo is http://weibo.com/javaee6
从上面两个例子中我们可以看出Message.format对用两个单引号括起来的表示不格式化。如果我们的字符串中有单引号那怎么办,很简单转义下就可以了。两个连接的单引号代表一个单引号。看下面的例子
String s1="my blogWebSite is ‘‘{0}‘‘ and sinaWeiBo is {1} "; String s2=MessageFormat.format(s1,"http://www.3lai8.com","http://weibo.com/javaee6"); System.out.println(s2);
结果打印
my blogWebSite is ‘http://www.3lai8.com‘ and sinaWeiBo is http://weibo.com/javaee6
单引号的问题就此结束,更加详细的MessageFormat的方法请参考java的api文档。这个说的非常的详细了,这里就不写出来了。
1 官方jdk6中关于MessageFormat的api:http://docs.oracle.com/javase/6/docs/api/java/text/MessageFormat.html
http://tool.oschina.net/apidocs/apidoc?api=jdk-zh
2 MessageFormat的用法 http://blog.csdn.net/zhiweianran/article/details/8666992
关于java中MessageFormat.format中单引号问题
时间: 2024-11-05 16:59:54