编程语言
首页 > 编程语言> > java去除下划线并首字母大写

java去除下划线并首字母大写

作者:互联网

(将如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;
    }

 

标签:newKey,ch,java,下划线,newMap,首字母,sbf,split,String
来源: https://www.cnblogs.com/i-tao/p/15330968.html