下滑线驼峰互转

 1 public class HumpUtils {
 2     private static final Pattern linePattern = Pattern.compile("_(\\w)");
 3     private static final Pattern humpPattern = Pattern.compile("[A-Z]");
 4 
 5     /**下划线转驼峰*/
 6     public static String lineHump(String str){
 7         str = str.toLowerCase();
 8         Matcher matcher = linePattern.matcher(str);
 9         StringBuffer sb = new StringBuffer();
10         while(matcher.find()){
11             matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
12         }
13         matcher.appendTail(sb);
14         return sb.toString();
15     }
16     /**驼峰转下划线*/
17     public static String hump2Line(String str){
18         Matcher matcher = humpPattern.matcher(str);
19         StringBuffer sb = new StringBuffer();
20         while(matcher.find()){
21             matcher.appendReplacement(sb, "_"+matcher.group(0).toLowerCase());
22         }
23         matcher.appendTail(sb);
24         return sb.toString();
25     }
26     public static void main(String[] args) {
27         String lineToHump = lineHump("45hhf_parent_no_leader");
28         System.out.println(lineToHump);
29         System.out.println(hump2Line(lineToHump));
30     }
31 }

 

转载于:https://www.cnblogs.com/stromluo/p/8306898.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值