下划线命名转驼峰

转小驼峰

java 复制代码
//下划线对小驼峰命名转换
public class UnderlineToCamelCase {
    public static String underlineToCamel(String underlineStr) {
        String[] words = underlineStr.split("_");
        StringBuilder result = new StringBuilder(words[0]);
        // 从第二个单词开始,将每个单词的首字母大写,并添加到结果中
        for (int i = 1; i < words.length; i++) {
            String word = words[i];
            if (word.length() > 0) {
                result.append(Character.toUpperCase(word.charAt(0)));
                if (word.length() > 1) {
                    result.append(word.substring(1));
                }
            }
        }
        return result.toString();
    }

    public static void main(String[] args) {
        System.out.println(underlineToCamel("user_name")); 
    }
}

转大驼峰

java 复制代码
public class UnderlineToBigCamelCase {
    public static String underlineToBigCamel(String underlineStr) {
        StringBuilder result = new StringBuilder();
        String[] words = underlineStr.split("_");
        // 遍历每个单词
        for (String word : words) {
            if (word.length() > 0) {
                result.append(Character.toUpperCase(word.charAt(0)));
                if (word.length() > 1) {
                    result.append(word.substring(1));
                }
            }
        }
        return result.toString();
    }

    public static void main(String[] args) {
        System.out.println(underlineToBigCamel("user_name")); 
    }
}
相关推荐
星辰徐哥3 分钟前
Rust函数与流程控制——构建逻辑清晰的系统级程序
开发语言·后端·rust
liliangcsdn3 分钟前
如何使用lambda对python列表进行排序
开发语言·python
李少兄6 分钟前
Git 忽略文件机制:.gitignore 与 .git/info/exclude
java·git·elasticsearch
☀Mark_LY16 分钟前
个人数据同步es小工具
java·elasticsearch
组合缺一28 分钟前
开发 Java MCP 就像写 Controller 一样简单,还支持 Java 8
java·人工智能·llm·solon·java8·mcp
tobias.b33 分钟前
408真题解析-2010-9-数据结构-折半查找的比较次数
java·数据结构·算法·计算机考研·408真题解析
源代码•宸33 分钟前
Leetcode—404. 左叶子之和【简单】
经验分享·后端·算法·leetcode·职场和发展·golang·dfs
WBluuue39 分钟前
数据结构与算法:dp优化——优化尝试和状态设计
c++·算法·leetcode·动态规划
java 乐山1 小时前
c 写一个文本浏览器(1)
c语言·开发语言
im_AMBER1 小时前
Leetcode 105 K 个一组翻转链表
数据结构·学习·算法·leetcode·链表