문서의 선택한 두 판 사이의 차이를 보여줍니다.
양쪽 이전 판 이전 판 다음 판 | 이전 판 마지막 판 양쪽 다음 판 | ||
java:string_format [2017/08/10 12:59] kwon37xi |
java:string_format [2019/02/22 20:26] kwon37xi |
||
---|---|---|---|
줄 1: | 줄 1: | ||
====== Java String Format ====== | ====== Java String Format ====== | ||
+ | |||
+ | ===== String.format() ===== | ||
+ | * [[https:// | ||
===== Slf4j MessageFormatter ===== | ===== Slf4j MessageFormatter ===== | ||
줄 8: | 줄 11: | ||
<code java> | <code java> | ||
+ | import org.slf4j.helpers.MessageFormatter; | ||
+ | |||
// sf의 뜻? simple format, slf4j format, ... | // sf의 뜻? simple format, slf4j format, ... | ||
+ | // 주의: " | ||
public static String sf(String messagePattern, | public static String sf(String messagePattern, | ||
return MessageFormatter.arrayFormat(messagePattern, | return MessageFormatter.arrayFormat(messagePattern, | ||
줄 48: | 줄 54: | ||
</ | </ | ||
+ | ===== org.apache.commons.lang.text.StrSubstitutor ===== | ||
+ | * [[https:// | ||
+ | |||
+ | <code java> | ||
+ | // 시스템 프라퍼티 | ||
+ | StrSubstitutor.replaceSystemProperties( | ||
+ | "You are running with java.version = ${java.version} and os.name = ${os.name}." | ||
+ | | ||
+ | // 일반적인 사용법 | ||
+ | Map valuesMap = HashMap(); | ||
+ | valuesMap.put(" | ||
+ | valuesMap.put(" | ||
+ | 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. | ||
+ | </ | ||