사용자 도구

사이트 도구


java:string_format

문서의 이전 판입니다!


Java String Format

Slf4j MessageFormatter

  • Slf4j의 log message formatter를 사용할 수 있다.
  • {}가 formatting anchor 이며, 이 부분이 파라미터 문자열로 대체된다.
  • Java의 MessageFormat보다 10배 정도 빠르다고 한다.
MessageFormatter.format("Set {1,2,3} is not equal to {}.", "1,2");
->  "Set {1,2,3} is not equal to 1,2."
 
// { 에대 한 escape 은 \\{
MessageFormatter.format("Set \\{} is not equal to {}.", "1,2");
-> "Set {} is not equal to 1,2."
 
// \ 자체를 사용하려면 \\\\
MessageFormatter.format("File name is C:\\\\{}.", "file.zip");
-> "File name is C:\file.zip"

MessageFormat

 int planet = 7;
 String event = "a disturbance in the Force";
 
 String result = MessageFormat.format(
     "At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.",
     planet, new Date(), event);
 
// 결과
 At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7.
java/string_format.1502332158.txt.gz · 마지막으로 수정됨: 2017/08/10 10:59 저자 kwon37xi