Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kahrl
Created May 22, 2011 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kahrl/985726 to your computer and use it in GitHub Desktop.
Save kahrl/985726 to your computer and use it in GitHub Desktop.
public static String initCap(String s) {
StringBuffer result = new StringBuffer(s.length());
boolean upper = true;
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if (Character.isWhitespace(c)) {
upper = true;
} else if (upper) {
c = Character.toUpperCase(c);
upper = false;
} else {
c = Character.toLowerCase(c);
}
result.append(c);
}
return result.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment