Java tutorial
//package com.java2s; public class Main { public static String wrap(String in, final int len) { in = in.trim(); if (in.length() < len) return in; if (in.substring(0, len).contains("\n")) return in.substring(0, in.indexOf("\n")).trim() + "\n\n" + wrap(in.substring(in.indexOf("\n") + 1), len); int place = Math.max(Math.max(in.lastIndexOf(" ", len), in.lastIndexOf("\t", len)), in.lastIndexOf("-", len)); return in.substring(0, place).trim() + "\n" + wrap(in.substring(place), len); } }