现在的位置: 首页 > 综合 > 正文

Java String 占位符

2013年10月10日 ⁄ 综合 ⁄ 共 534字 ⁄ 字号 评论关闭
String stringFormat  = "lexical error at position %s, encountered %s, expected %s ";  
      
String messageFormat ="lexical error at position {0}, encountered {1}, expected {2}";  
      
System.out.println(String.format(stringFormat, 123, 100, 456));  
      
System.out.println(MessageFormat.format(messageFormat, new Date(), 100, 456));  

2种方式 主要是占位符不一样,好看下结果是

lexical error at position 123, encountered 100, expected 456   
  
lexical error at position 10-10-12 下午9:35, encountered 100, expected 456 

看了下MessageFormat的api说明,这个占位符参数功能更加强大点,支持type,style等限定。所以如果需要使用高级功能建议是使用MessageFormat。

http://dikar.iteye.com/blog/783115

抱歉!评论已关闭.