Here you can find the source of wrapText(String s, int width, boolean noIterator)
public static String wrapText(String s, int width, boolean noIterator)
//package com.java2s; // it under the terms of the GNU General Public License as published by import java.text.BreakIterator; import java.util.StringTokenizer; public class Main { private static final int DEFAULT_WIDTH = 40; /**//from w w w. j a v a 2s. c o m * Utility function to wrap text at given width. Used mostly * for displaying text in the dialog box. * * @param s message string * @param width width per line * @return string with line feeds */ public static String wrapText(String str, int width) { if (str == null || str.length() < width) { return str; } String ret = ""; StringTokenizer tokenizer = new StringTokenizer(str, "\n"); while (tokenizer.hasMoreTokens()) { BreakIterator boundary = BreakIterator.getLineInstance(); String s = tokenizer.nextToken(); boundary.setText(s); int end; int start = 0; while ((end = boundary.next()) != BreakIterator.DONE) { if (end - start > width) { end = boundary.previous(); if (start == end) { // Was too long for even one iteration end = boundary.next(); } ret += s.substring(start, end); ret += "\n"; start = end; } } end = boundary.last(); ret = ret + s.substring(start, end) + "\n"; } return ret; } public static String wrapText(String s, int width, boolean noIterator) { if (noIterator) { if (s == null || s.length() <= width) { return s; } String ret = ""; int start = 0; int end = width; int len = s.length(); while (len > width) { ret += s.substring(start, end); ret += "\n"; len -= width; start += width; end += width; } ret += s.substring(start); return ret; } else { return wrapText(s, width); } } /** * Utility function to wrap text at default width. Used mostly * for displaying text in the dialog box. * * @param s message string * @return string with line feeds */ public static String wrapText(String s) { return (wrapText(s, DEFAULT_WIDTH)); } }