Java String 占位符

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

你可能感兴趣的:(java,Date,api,String)