Here you can find the source of wrapText(String line, int maxLineLength)
private static List<String> wrapText(String line, int maxLineLength)
//package com.java2s; //License from project: LGPL import java.util.ArrayList; import java.util.List; public class Main { private static List<String> wrapText(String line, int maxLineLength) { // TODO: This does not handle lines containing \n characters List<String> wrappedLines = new ArrayList<>(); while (line.length() >= maxLineLength) { int lastSpaceIndex = line.substring(0, maxLineLength).lastIndexOf(" "); if (lastSpaceIndex == -1) { break; }/*from w w w . j a v a 2 s. c o m*/ wrappedLines.add(line.substring(0, lastSpaceIndex)); line = line.substring(lastSpaceIndex + 1, line.length()); } wrappedLines.add(line); return wrappedLines; } }