Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a String to Modified Camel Case in Java or Title Case as is otherwise called [duplicate]

I want to convert any string to modified Camel case or Title case using some predefined libraries than writing my own functions.

For example "HI tHiS is SomE Statement" to "Hi This Is Some Statement"

Regex or any standard library will help me.

I found certain library functions in eclipse like STRING.toCamelCase(); is there any such thing existing?

like image 484
takrishna Avatar asked Jun 13 '13 02:06

takrishna


People also ask

Is camel case the same as title case?

Camel case is distinct from title case, which capitalises all words but retains the spaces between them, and from Tall Man lettering, which uses capitals to emphasize the differences between similar-looking product names such as "predniSONE" and "predniSOLONE".

What is CamelCase in Java?

CamelCase is a way to separate the words in a phrase by making the first letter of each word capitalized and not using spaces. It is commonly used in web URLs, programming and computer naming conventions. It is named after camels because the capital letters resemble the humps on a camel's back.


2 Answers

I used the below to solve this problem.

import org.apache.commons.lang.StringUtils;
StringUtils.capitalize(MyString);

Thanks to Ted Hopp for rightly pointing out that the question should have been TITLE CASE instead of modified CAMEL CASE.

Camel Case is usually without spaces between words.

like image 151
takrishna Avatar answered Sep 19 '22 20:09

takrishna


You can easily write the method to do that :

  public static String toCamelCase(final String init) {
    if (init == null)
        return null;

    final StringBuilder ret = new StringBuilder(init.length());

    for (final String word : init.split(" ")) {
        if (!word.isEmpty()) {
            ret.append(Character.toUpperCase(word.charAt(0)));
            ret.append(word.substring(1).toLowerCase());
        }
        if (!(ret.length() == init.length()))
            ret.append(" ");
    }

    return ret.toString();
}
like image 38
Florent Bayle Avatar answered Sep 18 '22 20:09

Florent Bayle