0

I have a String "adam levine". How to convert first letter of every word to uppercase like this "Adam Levine"?

String line = "adam levine";
line = line.substring(0, 1).toUpperCase() + line.substring(1);
System.out.println(line); // Adam levine
Shree
  • 354
  • 2
  • 21
Adam
  • 1,041
  • 1
  • 7
  • 22

1 Answers1

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();
}
ManojP
  • 6,113
  • 2
  • 37
  • 49