驼峰转下划线与下划线转驼峰
public class StringTools {
public static final String UNDERLINE = "_" ;
public static String underlineToHump ( String param, boolean flag) {
StringBuffer stringBuffer = null;
if ( param!= null) {
stringBuffer = new StringBuffer ( ) ;
String[ ] strings = param. split ( UNDERLINE) ;
if ( strings. length> 0 ) {
stringBuffer. append ( theFirstLetter ( strings[ 0 ] , flag) ) ;
for ( int i = 1 ; i < strings. length; i++ ) {
stringBuffer. append ( theFirstLetter ( strings[ i] , true ) ) ;
}
}
return stringBuffer. toString ( ) ;
}
return null;
}
public static String humpToUnderline ( String param) {
StringBuffer stringBuffer = null;
if ( param!= null) {
stringBuffer = new StringBuffer ( ) ;
char [ ] chars = param. toCharArray ( ) ;
stringBuffer. append ( chars[ 0 ] ) ;
for ( int i = 1 ; i < chars. length; i++ ) {
if ( chars[ i] >= 65 && chars[ i] <= 90 ) {
stringBuffer. append ( '_' ) ;
}
stringBuffer. append ( chars[ i] ) ;
}
return stringBuffer. toString ( ) ;
}
return null;
}
private static String theFirstLetter ( String param, boolean flag) {
if ( flag == true ) {
return param. substring ( 0 , 1 ) . toUpperCase ( ) + param. substring ( 1 ) ;
} else {
return param. substring ( 0 , 1 ) . toLowerCase ( ) + param. substring ( 1 ) ;
}
}
public static void main ( String[ ] args) {
System. out. println ( underlineToHump ( null, true ) ) ;
System. out. println ( underlineToHump ( "hello_word" , true ) ) ;
System. out. println ( underlineToHump ( "hello_word" , false ) ) ;
System. out. println ( underlineToHump ( "hello_word_earth" , true ) ) ;
System. out. println ( underlineToHump ( "hello_word_earth" , false ) ) ;
System. out. println ( "<---------------------------->" ) ;
System. out. println ( humpToUnderline ( null) ) ;
System. out. println ( humpToUnderline ( "helloWord" ) ) ;
System. out. println ( humpToUnderline ( "HelloWord" ) ) ;
System. out. println ( humpToUnderline ( "helloWordEarth" ) ) ;
System. out. println ( humpToUnderline ( "HelloWordEarth" ) ) ;
}
}