Java String Camel Case Format toCamelCase(String name)

Here you can find the source of toCamelCase(String name)

Description

to Camel Case

License

Open Source License

Declaration

public static String toCamelCase(String name) 

Method Source Code

//package com.java2s;
/**/*from   ww  w.j  ava  2 s. c  o m*/
 * LICENSING
 * 
 * This software is copyright by sunkid <sunkid@iminurnetz.com> and is
 * distributed under a dual license:
 * 
 * Non-Commercial Use:
 *    This program 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.
 *
 *    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Commercial Use:
 *    Please contact sunkid@iminurnetz.com
 */

public class Main {
    public static String toCamelCase(String name) {
        String[] words = constantCaseToEnglish(name).split(" ");
        StringBuilder s = new StringBuilder();
        for (String word : words) {
            s.append(firstToUpper(word));
        }
        return s.toString();
    }

    public static String constantCaseToEnglish(String constant) {
        String[] words = constant.split("_");
        StringBuilder english = new StringBuilder();
        for (String word : words) {
            english.append(word.toLowerCase());
            english.append(" ");
        }

        return english.substring(0, english.length() - 1);
    }

    public static String firstToUpper(String word) {
        if (word == null)
            return word;
        if (word.length() < 2)
            return word.toUpperCase();
        return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
    }
}

Related

  1. toCamelCase(String name)
  2. toCamelCase(String name)
  3. toCamelCase(String name)
  4. toCamelCase(String name)
  5. toCamelCase(String name)
  6. toCamelCase(String name)
  7. toCamelCase(String name)
  8. toCamelCase(String original)
  9. toCamelCase(String originalName)