First of all, please don't mark it as duplicate.
I have a very specific problem. I have to capitalize each word's first letter. Problem is that I can't find-out when a word start.
For example, I have given String:
0nd0-cathay bank (federal saving)
And output should be as following:
0Nd0-Cathay Bank (Federal Saving).
Currently I'm having following method for title case:
public static String toTitleCase(String str)
{
if (str == null)
return "";
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();
}
Thanks all.