Java String Camel Case Format toCamelCase(final String identifier, boolean capital)

Here you can find the source of toCamelCase(final String identifier, boolean capital)

Description

to Camel Case

License

Open Source License

Declaration

public static String toCamelCase(final String identifier, boolean capital) 

Method Source Code

//package com.java2s;
/*// w w w .j  av  a  2 s.c om
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Aaron Cake
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

public class Main {
    private static final StringBuilder STRING_BUILDER_INSTANCE = new StringBuilder();

    public static String toCamelCase(final String identifier, boolean capital) {
        if (identifier == null)
            return null;
        StringBuilder sb = new StringBuilder(identifier);
        if (sb.length() == 0)
            return "";
        int i = 0;
        while (i < sb.length()) {
            if (sb.charAt(i) == '_') {
                sb.delete(i, i + 1);
                if (sb.length() > i) {
                    sb.setCharAt(i, Character.toUpperCase(sb.charAt(i)));
                }
            } else {
                sb.setCharAt(i, Character.toLowerCase(sb.charAt(i)));
            }
            ++i;
        }
        if (capital && sb.length() > 0) {
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
        }
        return sb.toString();
    }

    /**
     * Concatenates a list of strings or objects to a single string.
     * WARNING: NOT THREAD SAFE
     * @param strings
     * @return the concatenated string
     */
    public static String toString(final Object... strings) {
        STRING_BUILDER_INSTANCE.setLength(0);
        for (final Object o : strings) {
            STRING_BUILDER_INSTANCE.append(String.valueOf(o));
        }
        return STRING_BUILDER_INSTANCE.toString();
    }
}

Related

  1. toCamelCase(final String init)
  2. toCamelCase(final String input, final char spacer)
  3. toCamelCase(final String inputString, boolean avoidFirst)
  4. toCamelCase(final String string)