驼峰和下划线互转

public class HumpLineUtil {
	
	 private static Pattern humpPattern = Pattern.compile("[A-Z]");  
	 private static Pattern linePattern = Pattern.compile("(_)(\\w)");  
     
     /**
      * 驼峰转下划线
      * @param str
      * @return
      */
     public static String humpToLine2(String str){  
         Matcher matcher = humpPattern.matcher(str);  
         StringBuffer sb = new StringBuffer();  
         while(matcher.find()){  
             matcher.appendReplacement(sb, "_"+matcher.group(0).toLowerCase());  
         }  
         matcher.appendTail(sb);  
         return sb.toString();  
     }  
     
     public static String humpToLine(String str){
         return str.replaceAll("[A-Z]", "_$0").toLowerCase();  
     }  
     
     /**
      * 下划线转驼峰
      * @param str
      * @return
      */
     public static String lineToHump(String str){
    	 //str = str.toLowerCase();  
         Matcher matcher = linePattern.matcher(str);  
         StringBuffer sb = new StringBuffer();  
         while(matcher.find()){  
             matcher.appendReplacement(sb, matcher.group(2).toUpperCase());  
         }  
         matcher.appendTail(sb);  
         return sb.toString();  
     }  

     public static void main(String[] args) {  
       String str="p_arentUuid";
        str= lineToHump(str);
       System.out.println(str);
     }  
     
}

猜你喜欢

转载自turbosky.iteye.com/blog/2319878
今日推荐