Convert a column name with underscores to the corresponding class name using "camel case". - Java java.lang

Java examples for java.lang:String Camel Case

Description

Convert a column name with underscores to the corresponding class name using "camel case".

Demo Code


import static org.mynah.oblatum.util.Constants.UNDERLINE;

public class Main{
    public static void main(String[] argv) throws Exception{
        String name = "java2s.com";
        System.out.println(convertUnderscoreNameToClassName(name));
    }// ww w . ja v  a2s. com
    /**
     * Convert a column name with underscores to the corresponding class name using "camel case".  A name
     * like "customer_number" would match a "CustomerNumber" class name.
     *
     * @param name the column name to be converted
     * @return the name using "camel case"
     */
    public static String convertUnderscoreNameToClassName(String name) {
        StringBuilder result = new StringBuilder();
        boolean nextIsUpper = false;
        if (name != null && name.length() > 0) {
            for (int i = 0; i < name.length(); i++) {
                String s = name.substring(i, i + 1);
                if (s.equals(UNDERLINE)) {
                    nextIsUpper = true;
                } else {
                    if (nextIsUpper || (i == 0)) {
                        result.append(s.toUpperCase());
                        nextIsUpper = false;
                    } else {
                        result.append(s.toLowerCase());
                    }
                }
            }
        }
        return result.toString();
    }
}

Related Tutorials