字符串转驼峰

字符串转驼峰

    public static String StringTransformation(String str){
        String result = str;
        if (str.contains("_")) {
            StringBuffer sb = new StringBuffer();
            Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
            Matcher matcher = pattern.matcher(str);
            boolean smallCamel = true;
            while (matcher.find()) {
                String word = matcher.group();
                sb.append(smallCamel && matcher.start() == 0 ? Character.toLowerCase(word.charAt(0))
                        : Character.toUpperCase(word.charAt(0)));
                int index = word.lastIndexOf('_');
                if (index > 0) {
                    sb.append(word.substring(1, index).toLowerCase());
                } else {
                    sb.append(word.substring(1).toLowerCase());
                }
            }
            result = sb.toString();
        } else {
            result = str.toLowerCase();
        }
        return result;
    }

 

posted @ 2019-04-02 09:44 A点点圈圈A 阅读( ...) 评论( ...) 编辑 收藏

你可能感兴趣的:(字符串转驼峰)