cdm/src/main/java/ucar/unidata/util/StringUtil2.java
public static String breakTextAtWords(String text, String insert, int lineSize) {
    StringBuilder buff = new StringBuilder();
    StringTokenizer stoker = new StringTokenizer(text);
    int lineCount = 0;
    while (stoker.hasMoreTokens()) {
      String tok = stoker.nextToken();
      if (tok.length() + lineCount >= lineSize) {
        buff.append(insert);
        lineCount = 0;
      }
      buff.append(tok);
      buff.append(" ");
      lineCount += tok.length() + 1;
    }

    return buff.toString();
  }
public static String breakTextAtWords(String text, String insert, int lineSize) {
    StringBuilder buff = new StringBuilder();
    StringTokenizer stoker = new StringTokenizer(text);
    int lineCount = 0;
    while (stoker.hasMoreTokens()) {
      String tok = stoker.nextToken();
      if (tok.length() + lineCount >= lineSize) {
        buff.append(insert);
        lineCount = 0;
      }
      buff.append(tok);
      buff.append(" ");
      lineCount += tok.length() + 1;
    }

    return buff.toString();
  }
public static String breakTextAtWords ( String text , String insert , int lineSize ) { StringBuilder buff = new StringBuilder ( ) ; StringTokenizer stoker = new StringTokenizer ( text ) ; int lineCount = 0 ; while ( stoker . hasMoreTokens ( ) ) { String tok = stoker . nextToken ( ) ; if ( tok . length ( ) + lineCount >= lineSize ) { buff . append ( insert ) ; lineCount = 0 ; } buff . append ( tok ) ; buff . append ( " " ) ; lineCount += tok . length ( ) + 1 ; } return buff . toString ( ) ; }
Break the given text into lines, respecting word boundaries (blank space).

@param text     The text to convert
@param insert   break to insert
@param lineSize line size to insert at
@return The text with added br tags.
Break the given text into lines respecting word boundaries ( blank space ) .