문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 다음 판 | 이전 판 | ||
|
java:string_format [2016/12/15 08:44] kwon37xi 만듦 |
java:string_format [2019/02/22 20:27] (현재) kwon37xi |
||
|---|---|---|---|
| 줄 1: | 줄 1: | ||
| ====== Java String Format ====== | ====== Java String Format ====== | ||
| + | |||
| + | ===== String.format() ===== | ||
| + | * [[https:// | ||
| + | |||
| + | ===== Slf4j MessageFormatter ===== | ||
| + | * [[https:// | ||
| + | * [[java: | ||
| + | * '' | ||
| + | * Java의 '' | ||
| + | |||
| + | <code java> | ||
| + | import org.slf4j.helpers.MessageFormatter; | ||
| + | |||
| + | // sf의 뜻? simple format, slf4j format, ... | ||
| + | // 주의: " | ||
| + | public static String sf(String messagePattern, | ||
| + | return MessageFormatter.arrayFormat(messagePattern, | ||
| + | } | ||
| + | |||
| + | // 위 sf 메소드를 static import 하여 사용한다. | ||
| + | |||
| + | String message = sf(" | ||
| + | </ | ||
| + | |||
| + | 포맷팅 예제 | ||
| + | < | ||
| + | sf(" | ||
| + | -> "Set {1,2,3} is not equal to 1,2." | ||
| + | |||
| + | // { 에대 한 escape 은 \\{ | ||
| + | sf(" | ||
| + | -> "Set {} is not equal to 1,2." | ||
| + | |||
| + | // \ 자체를 사용하려면 \\\\ | ||
| + | sf(" | ||
| + | -> "File name is C: | ||
| + | |||
| + | </ | ||
| ===== MessageFormat ===== | ===== MessageFormat ===== | ||
| 줄 11: | 줄 49: | ||
| " | " | ||
| | | ||
| + | |||
| + | // 결과 | ||
| + | At 12:30 PM on Jul 3, 2053, there was a disturbance in the Force on planet 7. | ||
| </ | </ | ||
| + | ===== 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. | ||
| + | </ | ||