eg: player_id -> playerId; player_name -> playerName; - Java java.lang

Java examples for java.lang:String Camel Case

Description

eg: player_id -> playerId; player_name -> playerName;

Demo Code


//package com.java2s;

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

    public static String underScore2CamelCase(String strs) {

        if (strs == null || !strs.contains("_")) {
            return strs;
        }
        StringBuilder sb = new StringBuilder("");
        String[] elems = strs.split("_");
        for (int i = 0; i < elems.length; i++) {
            String elem = elems[i].toLowerCase();
            if (i != 0) {
                char first = elem.toCharArray()[0];
                sb.append((char) (first - 32)).append(elem.substring(1));
            } else {
                sb.append(elem);
            }
        }
        return sb.toString();
    }
}

Related Tutorials