Convert name of format THIS_IS_A_NAME to ThisIsAName - Java java.lang

Java examples for java.lang:String Camel Case

Description

Convert name of format THIS_IS_A_NAME to ThisIsAName

Demo Code


//package com.java2s;

public class Main {
    /**//from w  w  w.j  av  a  2  s. co  m
     * 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 classes,
     *         converted from SQL name
     */
    public static String toJavaClassName(String sqlNotation) {
        StringBuilder sb = new StringBuilder();
        char[] buf = sqlNotation.toCharArray();
        for (int i = 0; i < buf.length; i++) {
            char c = buf[i];
            if (i == 0) {
                sb.append(buf[i]);
            } else if (c != '_') {
                sb.append(Character.toLowerCase(c));
            } else {
                i++;
                if (i < buf.length) {
                    sb.append(buf[i]);
                }
            }
        }
        return sb.toString();
    }
}

Related Tutorials