public static String formatParagraph ( final String src , final int len , final boolean breakOnWhitespace ) { StringBuilder str = new StringBuilder ( ) ; int total = src . length ( ) ; int from = 0 ; while ( from < total ) { int to = from + len ; if ( to >= total ) { to = total ; } else if ( breakOnWhitespace ) { int ndx = StringUtil . lastIndexOfWhitespace ( src , to - 1 , from ) ; if ( ndx != - 1 ) { to = ndx + 1 ; } } int cutFrom = StringUtil . indexOfNonWhitespace ( src , from , to ) ; if ( cutFrom != - 1 ) { int cutTo = StringUtil . lastIndexOfNonWhitespace ( src , to - 1 , from ) + 1 ; str . append ( src , cutFrom , cutTo ) ; } str . append ( ' ' ) ; from = to ; } return str . toString ( ) ; }
Formats provided string as paragraph .