Created
September 7, 2018 06:11
-
-
Save sisodiakaran/02790acf7cb90fa38a1640bbf4f10028 to your computer and use it in GitHub Desktop.
Android String to Title Case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String toTitleCase(String str) { | |
if (str == null) { | |
return null; | |
} | |
boolean space = true; | |
StringBuilder builder = new StringBuilder(str); | |
final int len = builder.length(); | |
for (int i = 0; i < len; ++i) { | |
char c = builder.charAt(i); | |
if (space) { | |
if (!Character.isWhitespace(c)) { | |
// Convert to title case and switch out of whitespace mode. | |
builder.setCharAt(i, Character.toTitleCase(c)); | |
space = false; | |
} | |
} else if (Character.isWhitespace(c)) { | |
space = true; | |
} else { | |
builder.setCharAt(i, Character.toLowerCase(c)); | |
} | |
} | |
return builder.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment