簡單的string佔位符替換


Java代碼  收藏代碼
  1.  String stringFormat  = "lexical error at position %s, encountered %s, expected %s ";  
  2.   
  3. String messageFormat ="lexical error at position {0}, encountered {1}, expected {2}";  
  4.   
  5. System.out.println(String.format(stringFormat, 123100456));  
  6.   
  7. System.out.println(MessageFormat.format(messageFormat, new Date(), 100456));  

   2種方式 主要是佔位符不一樣,好看下結果是

Java代碼  收藏代碼
  1. lexical error at position 123, encountered 100, expected 456   
  2.   
  3. lexical error at position 10-10-12 下午9:35, encountered 100, expected 456  

 

   看了下MessageFormat的api說明,這個佔位符參數功能更加強大點,支持type,style等限定。所以如果需要使用高級功能建議是使用MessageFormat。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章