Java String Camel Case Format toCamelCase(final String input, final char spacer)

Here you can find the source of toCamelCase(final String input, final char spacer)

Description

Converts a given String to camelCase AbcDef You can choose a character as "spacer" character, e.g.

License

Open Source License

Parameter

Parameter Description
input - the input String
spacer - the character used as spacer (e.g. "_")

Return

the camelCased input String

Declaration

public static String toCamelCase(final String input, final char spacer) 

Method Source Code

//package com.java2s;
/*//from   ww  w . j  a v a2 s  .  co m
 * Copyright (C) 2012 akquinet AG
 *
 * This file is part of the Forge Hibersap Plugin.
 *
 * The Forge Hibersap Plugin is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * The Forge Hibersap Plugin 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 Lesser
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with the Forge Hibersap Plugin. If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Converts a given String to camelCase AbcDef
     * 
     * You can choose a character as "spacer" character, e.g. "_"
     * Exapmles: ABC_DEF, Abc_Def and abc_def
     * 
     * @param input - the input String
     * @param spacer - the character used as spacer (e.g. "_")
     * @return the camelCased input String
     */
    public static String toCamelCase(final String input, final char spacer) {
        if (input == null) {
            return null;
        }
        if (input.isEmpty()) {
            return "";
        }

        final StringBuilder stringBuilder = new StringBuilder();

        for (final String subString : input.split(Character.toString(spacer))) {
            stringBuilder.append(subString.substring(0, 1).toUpperCase());
            stringBuilder.append(subString.substring(1).toLowerCase());
        }

        return stringBuilder.toString();
    }
}

Related

  1. toCamelCase(final String identifier, boolean capital)
  2. toCamelCase(final String init)
  3. toCamelCase(final String inputString, boolean avoidFirst)
  4. toCamelCase(final String string)
  5. toCamelcase(final String string)
  6. toCamelCase(final String string)