駝峯命名去下劃線首字母大寫

(將如TEST_TB_KKK_LLLL 轉換爲    testTbKkkLlll)

public static Map<String, Object> zh(Map<String,Object> map) {
        HashMap<String, Object> newMap = new HashMap<String, Object>();
        for (Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = (String) entry.getValue();
            String newKey = key.toLowerCase();
            StringBuffer sbf = new StringBuffer();
            if (newKey.contains("_"))
            {
                // 按下劃線來切割字符串爲數組
                String[] split = newKey.split("_");
                // 循環數組操作其中的字符串
                for (int i = 0, index = split.length; i < index; i++){
                    char[] ch = split[i].toCharArray();
                    if(i>0){
                        ch[0] = (char) (ch[0] - 32);
                    }
                    // 添加到字符串緩衝區
                    sbf.append(ch);
                }
            }else{
                sbf.append(newKey);
            }
            newMap.put(sbf.toString(), value);
        }
        return newMap;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章