Java method hump conversion method and underline character string

java method hump method and underline character string conversion 

. 1
Import java.util.regex.Matcher; 2 Import java.util.regex.Pattern; . 3 . 4 public class CamelAndUnderLineConverter { . 5 Private static the Pattern of Pattern.compile linePattern = ( "_ ( W \\) " ); . 6 . 7 / ** . 8 * underlined transfected hump . 9 * / 10 public static String lineToHump (String STR) { . 11 STR = str.toLowerCase (); 12 is Matcher Matcher = linePattern.matcher (STR); 13 StringBuffer SB =new new the StringBuffer (); 14 the while (matcher.find ()) { 15 matcher.appendReplacement (SB, Matcher.group (. 1 ) .toUpperCase ()); 16 } . 17 matcher.appendTail (SB); 18 is return sb.toString ( ); 19 } 20 is 21 is / ** 22 * hump turn underscore (simple wording, efficiency is lower than { @link # humpToLine2 (String)}) 23 is * / 24 public static String humpToLine (String STR) { 25 return str.replaceAll ( "[AZ]", "_ $ 0" ) .toLowerCase (); 26 is } 27 28 Private static the Pattern of Pattern.compile humpPattern = ( "[AZ]" ); 29 30 / ** 31 is * hump underlined turn, higher efficiency than the above 32 * / 33 is public static String humpToLine2 (String STR) { 34 is Matcher Matcher = humpPattern.matcher (STR); 35 the StringBuffer SB = new new the StringBuffer (); 36 the while (matcher.find ()) { 37 [ matcher.appendReplacement (SB, "_" + Matcher.group (0 ) .toLowerCase ()); 38 is } 39 matcher.appendTail (SB); 40 return sb.toString(); 41 } 42 43 public static void main(String[] args) { 44 String lineToHump = lineToHump("f_parent_no_leader"); 45 System.out.println(lineToHump);// fParentNoLeader 46 System.out.println(humpToLine(lineToHump));// f_parent_no_leader 47 System.out.println(humpToLine2(lineToHump));// f_parent_no_leader 48 } 49 }

 

Guess you like

Origin www.cnblogs.com/yangxiaobo-blog/p/11510937.html