22

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?

takrishna
  • 4,884
  • 3
  • 18
  • 35
  • 3
    Perhaps you mean [title case](http://en.wikipedia.org/wiki/Sentence_case#Title_case)? "[Camel case](http://en.wikipedia.org/wiki/CamelCase)" usually refers to things like "HiThisIsSomeStatement" (no delimiters between the words). – Ted Hopp Jun 13 '13 at 02:47
  • Sorry I didn't know about title case. Thanks Ted Hopp. I want Title Case – takrishna Jun 13 '13 at 03:34

4 Answers4

24

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();
}
Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
Florent Bayle
  • 11,520
  • 4
  • 34
  • 47
  • 6
    Instead of `word.substring(0, 1).toUpperCase()` you could do `Character.toUpperCase(word.charAt(0))` – fge Jun 13 '13 at 04:42
  • @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. – tez Jun 30 '22 at 03:22
24

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.

takrishna
  • 4,884
  • 3
  • 18
  • 35
21

From commons-lang3

org.apache.commons.lang3.text.WordUtils.capitalizeFully(String str)
superEb
  • 5,613
  • 35
  • 38
2

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));
  }
Community
  • 1
  • 1
NFE
  • 1,147
  • 1
  • 9
  • 22
  • 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. – Florent Bayle Jun 13 '13 at 03:20
  • @FlorentBayle oh let see updated answer!! – NFE Jun 13 '13 at 03:38