2017-12-28 7 views
0

문자열 "adam levine"이 있습니다. 이 "Adam Levine"처럼 모든 단어의 첫 글자를 대문자로 변환하는 방법은 무엇입니까?형식 변환 전체 이름 패턴 문자열

String line = "adam levine"; 
line = line.substring(0, 1).toUpperCase() + line.substring(1); 
System.out.println(line); // Adam levine 

답변

3
private static final Pattern bound = Pattern.compile("\\b(\\w)"); 

public static String titleize(final String input) { 
    StringBuffer sb = new StringBuffer(input.length()); 
    Matcher mat = bound.matcher(input); 
    while (mat.find()) { 
     mat.appendReplacement(sb, mat.group().toUpperCase()); 
    } 
    mat.appendTail(sb); 
    return sb.toString(); 
} 
+0

이 작업 좋다. "\\ b (\\ w)"에 대해 더 설명해 주시겠습니까? – Adam

+1

\\ w는 단어를 나타내며 \\ b는 단어 경계를 나타냅니다. – ManojP

관련 문제