Convert name of format THIS_IS_A_NAME to thisIsAName For each letter: if not '_' then convert to lower case and add to output string if '_' then skip letter and add next letter to output string without converting to lower case - Android java.lang

Android examples for java.lang:String Case

Description

Convert name of format THIS_IS_A_NAME to thisIsAName For each letter: if not '_' then convert to lower case and add to output string if '_' then skip letter and add next letter to output string without converting to lower case

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot./*  www. j  a  v  a  2s. c  om*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
//package com.java2s;

public class Main {
    /**
     * Convert name of format THIS_IS_A_NAME to thisIsAName For each letter: if
     * not '_' then convert to lower case and add to output string if '_' then
     * skip letter and add next letter to output string without converting to
     * lower case
     * 
     * @param sqlNotation
     * @return A name complaint with naming convention for Java methods and
     *         fields, converted from SQL name
     */
    public static String toJavaMethodName(String sqlNotation) {
        StringBuilder dest = new StringBuilder();
        char[] src = sqlNotation.toCharArray();

        for (int i = 0; i < src.length; i++) {
            char c = src[i];
            boolean isFirstChar = (i == 0) ? true : false;

            if (isFirstChar || c != '_') {
                dest.append(Character.toLowerCase(c));
            } else {
                i++;
                if (i < src.length) {
                    dest.append(src[i]);
                }
            }
        }
        return dest.toString();
    }
}

Related Tutorials