사용자 도구

사이트 도구


java:string_format

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:string_format [2017/08/10 12:14]
kwon37xi [Slf4j MessageFormatter]
java:string_format [2019/02/22 20:27] (현재)
kwon37xi
줄 1: 줄 1:
 ====== Java String Format ====== ====== Java String Format ======
 +
 +===== String.format() =====
 +  * [[https://dzone.com/articles/java-string-format-examples|Java String Format Examples]]
  
 ===== Slf4j MessageFormatter ===== ===== Slf4j MessageFormatter =====
-  * [[https://www.slf4j.org/api/org/slf4j/helpers/MessageFormatter.html||Slf4j MessageFormatter]] +  * [[https://www.slf4j.org/api/org/slf4j/helpers/MessageFormatter.html|Slf4j MessageFormatter]] 
-  * [[java:slf4j|Slf4j]]의 log message formatter를 사용할 수 있다.+  * [[java:slf4j|Slf4j]]의 log message formatter를 사용할 수 있다. 사용법이 매우 간단하다.
   * ''{}''가 formatting anchor 이며, 이 부분이 파라미터 문자열로 대체된다.   * ''{}''가 formatting anchor 이며, 이 부분이 파라미터 문자열로 대체된다.
   * Java의 ''MessageFormat''보다 10배 정도 빠르다고 한다.   * Java의 ''MessageFormat''보다 10배 정도 빠르다고 한다.
  
 <code java> <code java>
-</code> +import org.slf4j.helpers.MessageFormatter; 
-/** + 
- * Slf4j의 문자열 포맷 형태로 <code>sf("hi {}hello {}""there", "world");</code> 처럼 호출하여 메시지를 포매팅한다.<br> +// sf의 뜻? simple formatslf4j format, ... 
- * <em>주의: "{}"와 실제 인자 갯수가 달라도 오류 없이 지나간다.</em> +// 주의: "{}"와 실제 인자 갯수가 달라도 오류 없이 지나간다.
- * @see MessageFormatter +
- */+
 public static String sf(String messagePattern, Object... args) { public static String sf(String messagePattern, Object... args) {
     return MessageFormatter.arrayFormat(messagePattern, args).getMessage();     return MessageFormatter.arrayFormat(messagePattern, args).getMessage();
줄 21: 줄 22:
  
 String message = sf("Hello {}", "world!"); String message = sf("Hello {}", "world!");
 +</code>
 +
 +포맷팅 예제
 <code> <code>
-MessageFormatter.format("Set {1,2,3} is not equal to {}.", "1,2");+sf("Set {1,2,3} is not equal to {}.", "1,2");
 ->  "Set {1,2,3} is not equal to 1,2." ->  "Set {1,2,3} is not equal to 1,2."
  
 // { 에대 한 escape 은 \\{ // { 에대 한 escape 은 \\{
-MessageFormatter.format("Set \\{} is not equal to {}.", "1,2");+sf("Set \\{} is not equal to {}.", "1,2");
 -> "Set {} is not equal to 1,2." -> "Set {} is not equal to 1,2."
  
 // \ 자체를 사용하려면 \\\\ // \ 자체를 사용하려면 \\\\
-MessageFormatter.format("File name is C:\\\\{}.", "file.zip");+sf("File name is C:\\\\{}.", "file.zip");
 -> "File name is C:\file.zip" -> "File name is C:\file.zip"
  
줄 50: 줄 54:
 </code> </code>
  
 +===== org.apache.commons.lang.text.StrSubstitutor =====
 +  * [[https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/StrSubstitutor.html|StrSubstitutor (Apache Commons Lang)]]
 +  * ''$'' escape -> ''$$''
 +<code java>
 +// 시스템 프라퍼티
 +StrSubstitutor.replaceSystemProperties(
 +    "You are running with java.version = ${java.version} and os.name = ${os.name}.");
 +    
 +// 일반적인 사용법
 +Map valuesMap = HashMap();
 +valuesMap.put("animal", "quick brown fox");
 +valuesMap.put("target", "lazy dog");
 +String templateString = "The ${animal} jumped over the ${target}.";
 +StrSubstitutor sub = new StrSubstitutor(valuesMap);
 +String resolvedString = sub.replace(templateString);
 +
 +// 결과
 +The quick brown fox jumped over the lazy dog.
 +</code>
  
java/string_format.1502336679.txt.gz · 마지막으로 수정됨: 2017/08/10 12:14 저자 kwon37xi