Java String Camel Case camelCase(String string, boolean firstUpper)

Here you can find the source of camelCase(String string, boolean firstUpper)

Description

Returns the specified string into "camel case" : each word are appended without white-spaces, but with a capital letter.
Note that, except for the first word, if the whole word is uppurcase, it will be converted into lowercase.

License

Open Source License

Parameter

Parameter Description
firstUpper <code>true</code> if the first letter has to be uppercase.
string the string to transform into camel case

Return

the camel case string

Declaration

public static String camelCase(String string, boolean firstUpper) 

Method Source Code

//package com.java2s;
/*//from w w w . j a v  a 2s .c  o  m
  * (c) Copyright 2010-2011 AgileBirds
  *
  * This file is part of OpenFlexo.
  *
  * OpenFlexo is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * OpenFlexo is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>.
  *
  */

public class Main {
    /**
      * Returns the specified string into "camel case" : each word are appended without white-spaces, but with a capital letter.<br/>
      * Note that, except for the first word, if the whole word is uppurcase, it will be converted into lowercase.
      * 
      * Example : "Todo list" => "TodoList"; "DAO controller" => "daoController".
      * 
      * @param firstUpper <code>true</code> if the first letter has to be uppercase.
      * @param string the string to transform into camel case
      * @return the camel case string
      */
    public static String camelCase(String string, boolean firstUpper) {
        if (string == null)
            return null;
        String value = string.trim().replace('_', ' ');
        if (value.trim().length() == 0)
            return value;
        if (value.equals(value.toUpperCase()))
            value = value.toLowerCase();
        StringBuilder result = new StringBuilder(value.length());

        String[] words = value.split(" ");

        // First word
        if (words[0].equals(words[0].toUpperCase())) {
            if (firstUpper)
                result.append(words[0]);// If the first word is upper case, and first letter must be uppercase, we keep all the word uppercase.
            else
                result.append(words[0].toLowerCase());// If the first word is upper case, and first letter must be lowercase, we set all the word lowercase.
        } else {
            if (firstUpper)
                result.append(firstUpper(words[0]));
            else
                result.append(firstsLower(words[0]));
        }

        // Other words
        for (int i = 1; i < words.length; i++) {
            if (words[i].equals(words[i].toUpperCase())) {
                result.append(words[i]);
            } else
                result.append(firstUpper(words[i]));
        }

        return result.toString();
    }

    /**
      * Sets the first char into upper case.
      * 
      * @param value the string to transform.
      * @return the same string with the first char upper case.
      */
    public static String firstUpper(String value) {
        if (value == null)
            return null;
        if (value.length() < 2)
            return value.toUpperCase();
        return value.substring(0, 1).toUpperCase() + value.substring(1);
    }

    /**
      * Sets the first char into lower case.<br/>
      * If the word has more than its first letter in upper case, the consecutive next upper case letters are also converted into lower case.
      * 
      * @param value the string to convert.
      * @return the same string, with its first upper case letters converted into lower case.
      */
    public static String firstsLower(String value) {
        if (value == null)
            return null;

        if (value.length() == 0)
            return value;

        int indexOfUpperCase = -1;
        for (char c : value.toCharArray()) {
            if (!Character.isUpperCase(c))
                break;
            indexOfUpperCase++;
        }

        if (indexOfUpperCase <= 0)
            indexOfUpperCase = 1;

        return value.substring(0, indexOfUpperCase).toLowerCase()
                + (value.length() > indexOfUpperCase ? value.substring(indexOfUpperCase) : "");
    }
}

Related

  1. camelCase(String s)
  2. camelCase(String s)
  3. camelCase(String s)
  4. CamelCase(String str)
  5. camelCase(String str)
  6. camelCase(String text)
  7. camelCase(String text)
  8. camelCase(String text)
  9. camelCase(String text)