Java tutorial
public class Utils { public static final String breakLines(String s, int width) { s = " " + s; int n = 0; boolean moreLines = true; StringBuffer buffer = new StringBuffer(); if ((s == null) || (s.length() <= width)) return s; while (moreLines) { for (int i = width; i > 0; i--) { if (s.charAt(n + i) == ' ') { buffer.append(s.substring(n, n + i) + "\n "); n = n + i + 1; break; } } if ((n + width) >= s.length()) { buffer.append(s.substring(n)); moreLines = false; } } return buffer.toString(); } }