Java String Wrap wordWrap(String entry)

Here you can find the source of wordWrap(String entry)

Description

word Wrap

License

LGPL

Declaration

public static List<String> wordWrap(String entry) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<String> wordWrap(String entry) {
        int ind = 0;
        List<String> out = new ArrayList<>();

        while (ind <= entry.length()) {
            int nind = ind + 40;
            if (nind < entry.length()) {
                if (entry.charAt(nind) != ' ') {
                    while (entry.charAt(nind) != ' ' && nind != 0) {
                        nind--;/*from   w w  w. j  ava  2  s  .co m*/
                    }
                    if (nind == 0) {
                        nind = ind + 40;
                    }
                }
                out.add(entry.substring(ind, nind));
                ind = nind + 1;
            } else {
                out.add(entry.substring(ind));
                break;
            }
        }

        return out;
    }
}

Related

  1. getWordWrappedText(int maxWidth, String... lines)
  2. getWrappedLine(String line, int lineLength)
  3. lineWrap(String text, int width, boolean shiftNewLines)
  4. wordWrap(final String rawString, final int lineLength)
  5. wordWrap(String input, int width)
  6. wrapString(String str)
  7. wrapString(String text, int maxLength)
  8. wrapText(String description)