参考 ResourceBundle StringManager和MessageFormat 本文只探究它们的用途,不探究实现。...properties文件内容大概如下,可以看到都是key-value形式的: LocalStrings.properties LocalStrings_fr.properties 其中的{0}是用来结合MessageFormat...MessageFormat 可以用来填充字符串中的{0}、{1}等占位符,具体情况看参考里的链接。
二、静态方法 java.text.MessageFormat.format 方法定义: String MessageFormat.format(String fmt, Object...args)... 入参fmt为MessageFormat模式参数。...示例——将数字1.23格式为1.2: double num = 1.23; String str = MessageFormat.format("{0,number,#.#}", num); 四、MessageFormat...MessageFormat.format("{0}{1}", 1, 2); // 结果12 MessageFormat.format("'{0}{1}", 1, 2); // 结果{0}{1} MessageFormat.format...cf = new ChoiceFormat(limit, format); MessageFormat mf = new MessageFormat("{0}"); mf.setFormatByArgumentIndex
(String pattern, Object ... arguments) MessageFormat本身与语言环境无关,而与用户提供给MessageFormat的模式和用于已插入参数的子格式模式有关...MessageFormat.format("{0}{1}", 1, 2); // 结果12 MessageFormat.format("'{0}{1}", 1, 2); // 结果{0}{1} MessageFormat.format...每调用一次MessageFormat.format方法,都会新创建MessageFormat的一个实例,相当于MessageFormat只使用了一次。...MessageFormat temp = new MessageFormat(pattern); return temp.format(arguments); } 因此若要多次格式同一个模式的字符串...,那么创建一个MessageFormat实例在执行格式化操作比较好些 String message = "oh, {0} is a pig"; MessageFormat messageFormat
---- 现象:对整数格式化会出现“,"符号 ---- 使用org.apache.ibatis.jdbc.SQL动态生成sql时候,为了灵活设置参数,使用了MessageFormat来格式化:...MessageFormat如何格式化整数 ---- java.text.MessageFormat#format(java.lang.String, java.lang.Object...)...如何避免被DecimalFormat格式化,防止产生本博文不正确的sql参数 ---- MessageFormat.format的所有参数转换为字符串类型,即显示格式化参数。...如: MessageFormat.format(sql.toString(), ""+ 10000); MessageFormat.format(sql.toString(), String.valueOf...(10000)); MessageFormat.format(sql.toString(), Long.toString(10000)); 等等; 小结 ---- java.text.MessageFormat
("Mary", "is woman"); } } 代码:Code Shape 设计模式 CodeShapeSample package pattern; import java.text.MessageFormat...}, Arrays.asList(name, value)); } } CodeShapePattern package pattern; import java.text.MessageFormat...= null) { for(Object param : params) { System.out.println(MessageFormat.format..., e.getMessage())); throw e; } finally { System.out.println(MessageFormat.format...= null) { for(Object param : params) { System.out.println(MessageFormat.format
woman"); } } Code Shape Design Pattern Code CodeShapeSample package pattern; import java.text.MessageFormat...}, Arrays.asList(name, value)); } } CodeShapePattern package pattern; import java.text.MessageFormat...= null) { for(Object param : params) { System.out.println(MessageFormat.format..., e.getMessage())); throw e; } finally { System.out.println(MessageFormat.format...= null) { for(Object param : params) { System.out.println(MessageFormat.format
= null) { MessageFormat messageFormat = getMessageFormat(bundle, code, locale);...if (messageFormat !...= null) { return messageFormat; } } }...* @return the resulting MessageFormat, or {@code null} if no message * defined for the given...>(); codeMap.put(code, localeMap); } MessageFormat
是的,不得不承认它的使用场景较少,本文下面会介绍下它和MessageFormat的一个使用场景。...MessageFormat:字符串格式化 MessageFormat提供了一种与语言无关(不管你在中国还是其它国家,效果一样)的方式生成拼接消息/拼接字符串的方法。使用它来构造显示给最终用户的消息。...@Test public void test10() { MessageFormat messageFormat = new MessageFormat("Hello, my name is {...注意事项 下面基于此示例,对MessageFormat的使用注意事项作出几点强调。...temp = new MessageFormat(pattern); return temp.format(arguments); } 可以清晰看到,该静态方法本质上还是构造了一个MessageFormat
nodePath,String nodeData) throws KeeperException, InterruptedException{ System.out.println(MessageFormat.format...("创建节点返回结果:{0}",result)); System.out.println(MessageFormat.format("完成创建节点:{0}, 数据:{1}",nodePath...data = new String(zk.getData(nodePath, false, queryStat(zk, nodePath))); System.out.println(MessageFormat.format...//zk.setData(path, data, version, cb, ctx); System.out.println(MessageFormat.format("完成修改节点,path...//zk.delete(path, version, cb, ctx); System.out.println(MessageFormat.format("完成删除节点,path:{0}
在MessageFormat.format方法中组装jason数据字符串:{code:"w1",des:"w2"},起止分别有左大括号和右大括号。...直接写的点位符位报错: java.lang.IllegalArgumentException : can't parse argument number MessageFormat 解决方法是将单引号把大括号包含起来...如下: String responseTemplate = "'{'code:\"{0}\",des:\"{1}\"'}'"; System.out.println(MessageFormat.format...个单引号进行转义: String responseTemplate = "'{'code:''{0}'',des:''{1}'''}'"; System.out.println(MessageFormat.format
这个问题是由spring resource bundle 并调用MessageFormat引起的,根本原因是MessageFormat会转义单引号。...因此,主要原因就是MessageFormat的问题了。...5.测试MessageFormat @Test public void testQuote() throws Exception{ String message = "I'm {0}...."; ryan = MessageFormat.format(message, "Ryan"); System.out.println(ryan); Assert.assertEquals...", ryan); } 通过测试用例可以发现,MessageFormat会转义(escape)单引号(quote)。因此,如果想要输出一个单引号就需要针对的用两个单引号来替换。
MessageFormat在NumberFormat和DateFormat的基础上提供了强大的占位符字符串的格式化功能,支持时间、货币、数字以及对象属性的格式化操作。...(pattern1, params); // (4)使用指定的本地化对象格式化信息 MessageFormat messageFormat = new MessageFormat...在(3)处,通过MessageFormat的format()方法格式化信息串。...它使用了系统默认的本地化对象,由于我们是中文平台,因此默认为Locale.CHINA 在(4)处,我们显式指定MessageFormat的本地化对象。...要解决这个问题,只须使用带占位符的格式化串作为资源文件的属性值并结合使用MessageFormat就可以满足要求了。
long end2 = System.currentTimeMillis(); for (int i = 0; i < 10000000; i++) { MessageFormat.format...)); System.out.println("String.format cost: " + (end2 - end1)); System.out.println("MessageFormat...使用MessageFormat String username = "Jack"; logger.info(MessageFormat.format("{0} login system...at {1,date,yyyy-MM-dd HH:mm:ss}", username, new Date())); MessageFormat则使用的是占位符,占位符可以配置的元素有以下几种,应该很好理解...然后看了一下CPU,MessageFormat也是要高一点的
进行替换 这个流程比较清晰简单,对于 MessageFormat.format 却发现一个诡异的问题,当text中包含单引号时,后面的不会被替换, 测试case如下 public String replace...(String text, Object... args) { return MessageFormat.format(text, args); } @Test...debug到源码去看下,然后发现在生成 MessageFormat对象的实现中,单引号内部有特殊用途,认为两个单引号之间的为一个整体,不做替换 String text = "hello {0}, welcome...; String ans = MessageFormat.format(text, "Lucy", new Date(), "HangZhou"); System.out.println(ans); /...改进++ 对上面的正则获取key,然后再调用 MessageFormat.format()的方式不满意,特别是后者的潜规则还不少,我们要实现一个纯粹的,高效的,可扩展的替换工具,应该这么玩?
; } } catch (IOException e) { log.error(MessageFormat.format("创建表{0}失败...; }catch (IOException e) { log.error(MessageFormat.format("为表的某个单元格赋值失败,tableName...,rowKey)); } }catch (IOException e) { log.error(MessageFormat.format(...,columnFamily)); } }catch (IOException e) { log.error(MessageFormat.format...; } }catch (IOException e) { log.error(MessageFormat.format("删除指定的表失败
messageFormat = this.resolveCode(code, locale); if (messageFormat !...= null) { synchronized(messageFormat) { return messageFormat.format...messageFormat = this.resolveCode(code, locale); if (messageFormat !...= null) { synchronized(messageFormat) { return messageFormat.format(new Object...接着分析它实现了AbstractMessageSource抽象类中的resolveCode方法如下: protected MessageFormat resolveCode(String code, Locale
("map: {0}", o)); return o; }).doOnSuccess(o -> { logger.info(MessageFormat.format("doOnSuccess...("emit an empty: {0}", o)); return Mono.empty(); }).map(o -> { logger.info(MessageFormat.format...("doOnNext: {0}", o)); }).doOnSuccess(o -> { logger.info(MessageFormat.format("doOnSuccess: {0}"...("doOnSuccess: {0}", o)); }).doOnError(e -> { logger.info(MessageFormat.format("doOnError: {0}",...("map: {0}", o)); return o; }).count( ).doOnSuccess(o -> { logger.info(MessageFormat.format
apex:param主要有两个用法: 对文本进行相关格式转换 传递参数 一.对文本进行相关格式转换:此种方式主要是apex:outputText的value支持和java中的MessageFormat...java api 的MessageFormat类:http://tool.oschina.net/uploads/apidocs/jdk-zh/java/text/MessageFormat.html...MessageFormat可以使用以下形式的模式: ?...通过api我们可以看出如果使用MessageFormat可以使用三种方式: {i};{i,type};{i,type,style} demo举例: 1 2 <apex
领取专属 10元无门槛券
手把手带您无忧上云