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

123,364

Solution 1

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.

Solution 2

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();
}

Solution 3

From commons-lang3

org.apache.commons.lang3.text.WordUtils.capitalizeFully(String str)

Solution 4

Refer:

    static String toCamelCase(String s){
           String[] parts = s.split(" ");
           String camelCaseString = "";
           for (String part : parts){
               if(part!=null && part.trim().length()>0)
              camelCaseString = camelCaseString + toProperCase(part);
               else
                   camelCaseString=camelCaseString+part+" ";   
           }
           return camelCaseString;
        }

        static String toProperCase(String s) {
            String temp=s.trim();
            String spaces="";
            if(temp.length()!=s.length())
            {
            int startCharIndex=s.charAt(temp.indexOf(0));
            spaces=s.substring(0,startCharIndex);
            }
            temp=temp.substring(0, 1).toUpperCase() +
            spaces+temp.substring(1).toLowerCase()+" ";
            return temp;

        }
  public static void main(String[] args) {
     String string="HI tHiS is   SomE Statement";
     System.out.println(toCamelCase(string));
  }
Share:
123,364

Related videos on Youtube

takrishna
Author by

takrishna

Web Developer (Javascript, HTML/CSS), Chrome Extension, Android

Updated on July 09, 2022

Comments

  • takrishna
    takrishna almost 2 years

    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?

    • Ted Hopp
      Ted Hopp almost 11 years
      Perhaps you mean title case? "Camel case" usually refers to things like "HiThisIsSomeStatement" (no delimiters between the words).
    • takrishna
      takrishna almost 11 years
      Sorry I didn't know about title case. Thanks Ted Hopp. I want Title Case
  • Florent Bayle
    Florent Bayle almost 11 years
    This code throws an StringIndexOutOfBoundsException if there are two consecutive white-spaces in the String (or if the String is empty). It throws a NullPointerException if the String is null.
  • NFE
    NFE almost 11 years
    @FlorentBayle oh let see updated answer!!
  • fge
    fge almost 11 years
    Instead of word.substring(0, 1).toUpperCase() you could do Character.toUpperCase(word.charAt(0))
  • Admin
    Admin almost 6 years
    not working in android studio java
  • takrishna
    takrishna about 5 years
    Have you imported the required Apache package - definitely this utility is not tied to any IDE - it is a Java utility
  • aswzen
    aswzen over 4 years
    Deprecated, replaced by org.apache.commons.text
  • tez
    tez almost 2 years
    @fge This is quite old comment but I want to know your thought process behind this. Can you please elaborate the difference between the two.