Here you can find the source of wrapString(String text, int maxLength)
public static List<String> wrapString(String text, int maxLength)
//package com.java2s; import java.util.ArrayList; import java.util.List; public class Main { public static List<String> wrapString(String text, int maxLength) { List<String> tmpStrings = new ArrayList<String>(); if (text == null) { return tmpStrings; }/*from w w w .j a va 2s . c om*/ text = text.trim(); if (text.length() <= maxLength) { tmpStrings.add(text); return tmpStrings; } while (text.length() > maxLength) { int i = maxLength; while (true) { if (text.charAt(i) == ' ') { break; } else if (i == 0) { i = maxLength; break; } i--; } tmpStrings.add(text.substring(0, i)); text = text.substring(i + 1).trim(); } if (text.length() > 0) { tmpStrings.add(text); } return tmpStrings; } }