문서의 이전 판입니다!
{}
가 formatting anchor 이며, 이 부분이 파라미터 문자열로 대체된다.MessageFormat
보다 10배 정도 빠르다고 한다./** * Slf4j의 문자열 포맷 형태로 sf("hi {}, hello {}", "there", "world"); 처럼 호출하여 메시지를 포매팅한다.<br> * <em>주의: "{}"와 실제 인자 갯수가 달라도 오류 없이 지나간다.</em> * @see MessageFormatter */ public static String sf(String messagePattern, Object... args) { return MessageFormatter.arrayFormat(messagePattern, args).getMessage(); } // 위 sf 메소드를 static import 하여 사용한다. String message = sf("Hello {}", "world!");
포맷팅 예제
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"
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.