1

how to convert a starting character of each string into capital letter. example:


input:time to think


Output:Time To Think

Atri
  • 5,511
  • 5
  • 30
  • 40
cv7
  • 15
  • 3
  • give me the quick one – cv7 Dec 22 '15 at 05:06
  • I answered [this question](http://stackoverflow.com/questions/34228942/from-camel-case-to-camelcase-in-java/34229081#34229081) recently and its quite a similar problem, take a look and let me know if you still need help with anything. – Noam Hacker Dec 22 '15 at 05:08

3 Answers3

1
public static String capitalizeString(String string) {
  char[] chars = string.toLowerCase().toCharArray();
  boolean found = false;
  for (int i = 0; i < chars.length; i++) {
    if (!found && Character.isLetter(chars[i])) {
      chars[i] = Character.toUpperCase(chars[i]);
      found = true;
    } else if (Character.isWhitespace(chars[i]) || chars[i]=='.' || chars[i]=='\'') { // You can add other chars here
      found = false;
    }
  }
  return String.valueOf(chars);
}

This should help you.

Adarsh Bhat
  • 159
  • 1
  • 12
1

WordUtils.capitalize(str) (from apache commons-lang)

(Note: if you need "fOO BAr" to become "Foo Bar", then use capitalizeFully(..) instead)

or you can also do it as,

         String str="hello how are you".trim();
         String[] arr = str.split(" ");
         StringBuffer sb = new StringBuffer();

         for (int i = 0; i < arr.length; i++) {
             sb.append(Character.toUpperCase(arr[i].charAt(0)))
                 .append(arr[i].substring(1)).append(" ");
         }

         System.out.print(sb.toString());
Zia
  • 1,001
  • 1
  • 13
  • 25
0

WordUtils.capitalize(str) in apache commons should do the trick http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html#capitalize(java.lang.String)

dolan
  • 1,716
  • 11
  • 22