문제

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.

도움이 되었습니까?

해결책

try this regex

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

it produces

Matthew S'mith
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top