public static String toCharCount ( final String aString , final int aCount ) { final StringBuilder builder = new StringBuilder ( ) ; final String [ ] words = aString . split ( "\\s" ) ; int count = 0 ; for ( final String word : words ) { count += word . length ( ) ; if ( count < aCount ) { builder . append ( word ) ; if ( ( count += 1 ) < aCount ) { builder . append ( ' ' ) ; } else { builder . append ( EOL ) . append ( DOUBLE_SPACE ) ; count = 2 ; } } else { builder . append ( EOL ) . append ( DOUBLE_SPACE ) . append ( word ) ; count = word . length ( ) + 2 ; // two spaces at start of line } } return builder . toString ( ) ; }
Formats a string with or without line breaks into a string with lines with less than a supplied number of characters per line .