To camel case from underscore. - Java java.lang

Java examples for java.lang:String Camel Case

Description

To camel case from underscore.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String underscore = "java2s.com";
        System.out.println(toCamelFromUnderscore(underscore));
    }//from  w w  w .java 2  s . co  m

    /**
     * To camel from underscore.
     * 
     * @param underscore
     *          the underscore
     * 
     * @return the string
     */
    public static String toCamelFromUnderscore(String underscore) {

        StringBuffer result = new StringBuffer();
        String[] vals = underscore.split("_");

        for (int i = 0; i < vals.length; i++) {
            String lower = vals[i].toLowerCase();
            if (i > 0) {
                char c = Character.toUpperCase(lower.charAt(0));
                result.append(c);
                result.append(lower.substring(1));
            } else {
                result.append(lower);
            }
        }

        return result.toString();
    }
}

Related Tutorials