Java String Wrap wrapString(String text, int maxLength)

Here you can find the source of wrapString(String text, int maxLength)

Description

wrap String

License

Open Source License

Declaration

public static List<String> wrapString(String text, int maxLength) 

Method Source Code

//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;
    }
}

Related

  1. lineWrap(String text, int width, boolean shiftNewLines)
  2. wordWrap(final String rawString, final int lineLength)
  3. wordWrap(String entry)
  4. wordWrap(String input, int width)
  5. wrapString(String str)
  6. wrapText(String description)
  7. wrapText(String inString, String newline, int wrapColumn)
  8. wrapText(String line, int maxLineLength)
  9. wrapText(String text, int len)