2017-12-28 10 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

関連する問題