Java String Camel Case Format toCamelCase(String name)

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

Description

Converts a string to camel case.

License

Apache License

Parameter

Parameter Description
name The string to convert to camel case.

Return

The string in camel case.

Declaration

public static String toCamelCase(String name) 

Method Source Code

//package com.java2s;
/*/*from   ww  w.jav a  2 s  .  c o m*/
 * Copyright The Sett Ltd, 2005 to 2014.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Converts a string to camel case.
     *
     * @param  name The string to convert to camel case.
     *
     * @return The string in camel case.
     */
    public static String toCamelCase(String name) {
        String[] parts = name.split("_");
        String result = parts[0];

        for (int i = 1; i < parts.length; i++) {
            if (parts[i].length() > 0) {
                result += upperFirstChar(parts[i]);
            }
        }

        return result;
    }

    /**
     * Converts the first character of a string to upper case.
     *
     * @param  name The string to convert the first character of.
     *
     * @return The string with its first character in upper case.
     */
    public static String upperFirstChar(String name) {
        return name.substring(0, 1).toUpperCase() + name.substring(1);
    }
}

Related

  1. toCamelCase(String input)
  2. toCamelCase(String input, boolean capitalizeFirsLetter)
  3. toCamelCase(String input, boolean firstCharUppercase, char separator)
  4. toCamelCase(String inputString)
  5. toCamelCase(String inputString)
  6. toCamelCase(String name)
  7. toCamelCase(String name)
  8. toCamelCase(String name)
  9. toCamelCase(String name)