Skip to content

Instantly share code, notes, and snippets.

@ponnamkarthik
Created October 15, 2017 20:00
Show Gist options
  • Save ponnamkarthik/90822962944a8fe765524159c476495e to your computer and use it in GitHub Desktop.
Save ponnamkarthik/90822962944a8fe765524159c476495e to your computer and use it in GitHub Desktop.
Convert Text to Titlecase in Java
public static String toTitleCase(String str)
{
String[] words = str.trim().split(" ");
StringBuilder ret = new StringBuilder();
for(int i = 0; i < words.length; i++)
{
if(words[i].trim().length() > 0)
{
Log.e("words[i].trim",""+words[i].trim().charAt(0));
ret.append(Character.toUpperCase(words[i].trim().charAt(0)));
ret.append(words[i].trim().substring(1));
if(i < words.length - 1) {
ret.append(' ');
}
}
}
return ret.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment