public static String uppercaseToUnderscore ( final String str ) { if ( str == null ) { return null ; } final StringBuffer sb = new StringBuffer ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { final char ch = str . charAt ( i ) ; if ( Character . isUpperCase ( ch ) ) { if ( i > 0 ) { sb . append ( "_" ) ; } sb . append ( Character . toLowerCase ( ch ) ) ; } else { sb . append ( ch ) ; } } return sb . toString ( ) ; }
Inserts an underscore before every upper case character and returns an all lower case string . If the first character is upper case an underscore will not be inserted .