3

Can anyone please help regarding camel casing strings that contain apostrophes, whereby I don't want the next letter after the apostrophe to be put to upper case.

My code reads from a txt file and then processes it as accordingly.

For example "MATTHEW SMITH" will be converted to "Matthew Smith" However "MATTHEW S'MITH" will be converted to "Matther S'Mith" when it should be "S'mith"

    public static String toCamelCase(String tmp){
    Pattern p = Pattern.compile(CAMEL_CASE_REG_EXP);
    Matcher m = p.matcher(tmp);
    StringBuffer result = new StringBuffer();
    String word;
    while (m.find()) 
    {
        word = m.group();

                    result.append(word.substring(0,1).toUpperCase()+word.substring(1).toLowerCase());

    }
    return result.toString();
}
public static final String CAMEL_CASE_REG_EXP = "([0-9]+)?([a-zA-Z]+)(\\')?(\\-)?(\\s)?";

Thanks in advance.

Charles
  • 50,943
  • 13
  • 104
  • 142
  • So you actually *want* "O'CONNOR" to resolve to "O'connor" rather than "O'Connor"? I understand wanting "YOU'RE" to resolve to "You're", but I wonder if this is a little more complicated -- you'll need a mild conditional to identify when it's appropriate to capitalize after an apostrophe, and when it isn't. – cabbagery May 21 '13 at 05:55

1 Answers1

1

try this regex

public static final String CAMEL_CASE_REG_EXP = "\\s*\\S+\\s*";

it produces

Matthew S'mith
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Had to use another if-else statement to ensure that it was only doing it for Strings that contained an apostrophe as some first names for example "Jiminy Cricket" and "Lake Side Street" was being changed to "Jiminy cricket" and "Lake side Street". – user2404110 May 21 '13 at 06:34