下划线命名转驼峰

转小驼峰

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")); 
    }
}
相关推荐
BUG研究员_4 小时前
Runnable与LCEL
开发语言·人工智能·python
码农颜5 小时前
5.4.1 锁分类
java·数据库·mysql
wabs6665 小时前
关于图论【最短路径之Dijkstra算法(堆优化版)|卡码网47.参加科学大会的思考】
数据结构·算法·图论·优先级队列·邻接表·小顶堆·卡码网
坚持编程的菜鸟6 小时前
编写判断大小端程序
c语言·算法·判断大小端
papaofdoudou6 小时前
判断排列逆序奇偶性的乘积判别法(范德蒙德符号法)
人工智能·算法
牛艺翔6 小时前
C++基础
开发语言·c++
键盘会跳舞6 小时前
C++ :容器适配器stack源码级拆解
开发语言·c++··stack·先进后出
linux-hzh6 小时前
百日算法修炼 · Day 03
java·算法
美味蛋炒饭.6 小时前
Git 版本控制(下)
开发语言·git·学习·总结·后端开发
yanaiding6 小时前
C# WPF 独立开发看图工具 PixSight 经验介绍(二)—— 水印模块开发实录
图像处理·c#·wpf·个人开发