Java String Camel Case Format toCamelCase(String name)

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

Description

Creates a capitalized, underscore delimited name from the given camel case name.

License

Open Source License

Parameter

Parameter Description
name the name

Return

the result name

Declaration

public static String toCamelCase(String name) 

Method Source Code

//package com.java2s;
/**/*from w w  w  . j  a  v  a  2 s . c om*/
 *         PlasmaSDO? License
 * 
 * This is a community release of PlasmaSDO?, a dual-license 
 * Service Data Object (SDO) 2.1 implementation. 
 * This particular copy of the software is released under the 
 * version 2 of the GNU General Public License. PlasmaSDO? was developed by 
 * TerraMeta Software, Inc.
 * 
 * Copyright (c) 2013, TerraMeta Software, Inc. All rights reserved.
 * 
 * General License information can be found below.
 * 
 * This distribution may include materials developed by third
 * parties. For license and attribution notices for these
 * materials, please refer to the documentation that accompanies
 * this distribution (see the "Licenses for Third-Party Components"
 * appendix) or view the online documentation at 
 * <http://plasma-sdo.org/licenses/>.
 *  
 */

public class Main {
    /**
     * Creates a capitalized, underscore delimited name
     * from the given camel case name. 
     * @param name the name
     * @return the result name
     */
    public static String toCamelCase(String name) {
        StringBuilder buf = new StringBuilder();
        if (name.indexOf("_") >= 0) {
            char[] array = name.toLowerCase().toCharArray();
            for (int i = 0; i < array.length; i++) {
                if (i > 0) {
                    if (array[i] == '_') {
                        int next = i + 1;
                        if (next < array.length)
                            array[next] = Character.toUpperCase(array[next]);
                        continue;
                    } else
                        buf.append(array[i]);
                } else {
                    buf.append(Character.toUpperCase(array[i]));
                }
            }
        } else {
            char[] array = name.toCharArray();
            if (hasUpper(array)) {
                if (hasLower(array)) {
                    buf.append(name);
                } else
                    buf.append(name.toLowerCase());
            } else {
                buf.append(name);
            }
        }

        return buf.toString();
    }

    private static boolean hasUpper(char[] array) {
        for (int i = 0; i < array.length; i++)
            if (Character.isUpperCase(array[i]))
                return true;
        return false;
    }

    private static boolean hasLower(char[] array) {
        for (int i = 0; i < array.length; i++)
            if (Character.isLowerCase(array[i]))
                return true;
        return false;
    }
}

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 name)
  9. toCamelCase(String original)