下划线命名转驼峰

转小驼峰

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")); 
    }
}
相关推荐
FL16238631295 分钟前
[C#][winform]基于yolov8的水表读数检测与识别系统C#源码+onnx模型+评估指标曲线+精美GUI界面
开发语言·yolo·c#
小白菜又菜2 小时前
Leetcode 3432. Count Partitions with Even Sum Difference
算法·leetcode
cnxy1883 小时前
围棋对弈Python程序开发完整指南:步骤1 - 棋盘基础框架搭建
开发语言·python
wuhen_n3 小时前
LeetCode -- 15. 三数之和(中等)
前端·javascript·算法·leetcode
sin_hielo3 小时前
leetcode 2483
数据结构·算法·leetcode
Nonoas3 小时前
动态代理:发布订阅的高级玩法
java·ide·intellij-idea
程序员-周李斌4 小时前
Java 死锁
java·开发语言·后端
Xの哲學4 小时前
Linux多级时间轮:高精度定时器的艺术与科学
linux·服务器·网络·算法·边缘计算
大头流矢4 小时前
归并排序与计数排序详解
数据结构·算法·排序算法
油泼辣子多加4 小时前
【信创】算法开发适配
人工智能·深度学习·算法·机器学习