public static String buitify ( String string ) { char [ ] chars = string . toCharArray ( ) ; StringBuilder sb = new StringBuilder ( ) ; int lastApplied = 0 ; for ( int i = 0 ; i < chars . length ; i ++ ) { char pCh = i > 0 ? chars [ i - 1 ] : 0 ; char ch = chars [ i ] ; if ( ch == ' ' || ch == ' ' || Character . isWhitespace ( ch ) ) { sb . append ( chars , lastApplied , i - lastApplied ) ; lastApplied = i + 1 ; } else if ( i == 0 && Character . isLowerCase ( ch ) ) { sb . append ( Character . toUpperCase ( ch ) ) ; lastApplied = i + 1 ; } else if ( i > 0 && Character . isUpperCase ( ch ) && ! ( Character . isWhitespace ( pCh ) || pCh == ' ' || pCh == ' ' ) && ! Character . isUpperCase ( pCh ) ) { sb . append ( chars , lastApplied , i - lastApplied ) . append ( ' ' ) . append ( ch ) ; lastApplied = i + 1 ; } else if ( i > 0 && Character . isLowerCase ( ch ) && ( Character . isWhitespace ( pCh ) || pCh == ' ' || pCh == ' ' ) ) { sb . append ( chars , lastApplied , i - lastApplied ) ; if ( sb . length ( ) > 0 ) sb . append ( ' ' ) ; sb . append ( Character . toUpperCase ( ch ) ) ; lastApplied = i + 1 ; } } sb . append ( chars , lastApplied , chars . length - lastApplied ) ; return sb . toString ( ) ; }
Utility method to make source string more human readable