驼峰式转下划线大写(包含对数字的判断)

提供了一个公共静态方法`underscoreName`,将驼峰式命名的字符串转换为下划线分隔的大写形式。首先,将首字母转为大写,然后遍历剩余字符,若当前字符为大写且非数字,则在其前插入下划线;如果字符为数字,也会在其前插入下划线。使用正则表达式检查是否为数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   以下是完整的接口,可作为工具调用!

public static String underscoreName(String name) {  
   StringBuilder result = new StringBuilder();  
   if (name != null && name.length() > 0) {  
       // 将第一个字符处理成大写  
       result.append(name.substring(0, 1).toUpperCase());  
       // 循环处理其余字符  
       for (int i = 1; i < name.length(); i++) {  
           String s = name.substring(i, i + 1);  
           // 在大写字母前添加下划线  
           if (s.equals(s.toUpperCase()) && !Character.isDigit(s.charAt(0))) {  
               result.append("_");  
           }  
           //在数字前添加下划线
           Pattern pattern = Pattern.compile("[0-9]*");
           Matcher isNum = pattern.matcher(s);
           if(isNum.matches() ){
            result.append("_");
           }
           // 其他字符直接转成大写  
           result.append(s.toUpperCase());  
       }  
   }  
   return result.toString();  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值