사용자 도구

사이트 도구


java:string_format

문서의 이전 판입니다!


Java String Format

Slf4j MessageFormatter

  • Slf4j의 log message formatter를 사용할 수 있다.
  • {}가 formatting anchor 이며, 이 부분이 파라미터 문자열로 대체된다.
  • Java의 MessageFormat보다 10배 정도 빠르다고 한다.
/**
 * Slf4j의 문자열 포맷 형태로 <code>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!”); </code> 포맷팅 예제 <code> 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” </code> ===== MessageFormat ===== * java.text.MessageFormat <code java> 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. </code>

java/string_format.1502336699.txt.gz · 마지막으로 수정됨: 2017/08/10 12:14 저자 kwon37xi