美文网首页
Java下划线字符串转驼峰字符串

Java下划线字符串转驼峰字符串

作者: 超音速6 | 来源:发表于2020-04-11 18:13 被阅读0次
    /**
     * 下划线
     */
    private static final char SEPARATOR = '_';
    
    /**
     * 驼峰式命名法
     * 例如:user_name->userName
     */
    public static String toCamelCase(String s) {
        if (s == null) {
            return null;
        }
        s = s.toLowerCase();
        StringBuilder sb = new StringBuilder(s.length());
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
    
            if (c == SEPARATOR) {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(c));
                upperCase = false;
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
    

    相关文章

      网友评论

          本文标题:Java下划线字符串转驼峰字符串

          本文链接:https://www.haomeiwen.com/subject/rzvumhtx.html