-2

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.

Harish Godara
  • 2,635
  • 1
  • 12
  • 29

2 Answers2

1

You can use the following solution with regex (not the prettiest though).

String input = "0nd0-cathay bank (federal saving)";
// case-sensitive Pattern:
//                              | group 1: first letter
//                              |      | group 2: any other character if applicable
//                              |      |    | word boundary
Pattern p = Pattern.compile("([a-z])(.*?)\\b");
// Pattern for all capital letters 
// Pattern p = Pattern.compile("([A-Z])(.*?)\\b");
// initializing StringBuffer for appending
StringBuffer sb = new StringBuffer();
Matcher m = p.matcher(input);
// iterating over matches
while (m.find()) {
   // appending replacement:
   //                      | 1st letter capitalized
   //                      |                        | concatenated with...
   //                      |                        |     | any other character
   //                      |                        |     | within word boundaries
   m.appendReplacement(sb, m.group(1).toUpperCase().concat(m.group(2)));
   // Replacement for all capital letters
   // m.appendReplacement(sb, m.group(1).concat(m.group(2).toLowerCase()));
}
// appending tail if any
m.appendTail(sb);
System.out.println(sb.toString());

Output

0Nd0-Cathay Bank (Federal Saving)

Notes

  • This will capitalize the first alphabetic character in all words, even if words do not start with an alphabetic character.
  • For instance, 0nd0 becomes 0Nd0.
  • If you don't want this edge case after all, prepend a word boundary (\\b) to the Pattern.
Mena
  • 45,491
  • 11
  • 81
  • 98
1

This might work for you :

public static void main(String[] args) {
    String s = "0nd0-cathay bank (federal saving)";
    Pattern p = Pattern.compile("(?<![a-z])([a-z])"); // negative look-behind. check if your char is not preceeded by another char.
    Matcher m = p.matcher(s);
    while (m.find()) {
        char val = m.group(1).toUpperCase().charAt(0);
        s = s.replaceFirst(m.group(1), String.valueOf(val));
    }
    System.out.println(s);
}

O/P :

0Nd0-Cathay Bank (Federal Saving)
TheLostMind
  • 34,842
  • 11
  • 64
  • 97
  • What `Regex` we should use if we have the string like: `0ND0-CATHAY BANK (FEDERAL SAVING)` and output should be: `0Nd0-Cathay Bank (Federal Saving)`. I don't know about `Regex` – Harish Godara Oct 07 '14 at 10:34