- 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。