Java String Wrap wrapText(String text, int len)

Here you can find the source of wrapText(String text, int len)

Description

wrap Text

License

Apache License

Parameter

Parameter Description
text a parameter
len a parameter

Declaration

public static String[] wrapText(String text, int len) 

Method Source Code


//package com.java2s;
/*/*from  ww w.j av  a2  s  . c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.*;

public class Main {
    /**
     * @param text
     * @param len
     * @return
     */
    public static String[] wrapText(String text, int len) {
        // return empty array for null text
        if (text == null)
            return new String[] {};

        // return text if len is zero or less
        if (len <= 0)
            return new String[] { text };

        // return text if less than length
        if (text.length() <= len)
            return new String[] { text };

        char[] chars = text.toCharArray();
        Vector lines = new Vector();
        StringBuilder line = new StringBuilder();
        StringBuilder word = new StringBuilder();

        for (char aChar : chars) {
            word.append(aChar);

            if (aChar == ' ') {
                if ((line.length() + word.length()) > len) {
                    lines.add(line.toString());
                    line.delete(0, line.length());
                }

                line.append(word);
                word.delete(0, word.length());
            }
        }

        // handle any extra chars in current word
        if (word.length() > 0) {
            if ((line.length() + word.length()) > len) {
                lines.add(line.toString());
                line.delete(0, line.length());
            }
            line.append(word);
        }

        // handle extra line
        if (line.length() > 0) {
            lines.add(line.toString());
        }

        String[] ret = new String[lines.size()];
        int c = 0; // counter
        for (Enumeration e = lines.elements(); e.hasMoreElements(); c++) {
            ret[c] = (String) e.nextElement();
        }

        return ret;
    }
}

Related

  1. wrapString(String str)
  2. wrapString(String text, int maxLength)
  3. wrapText(String description)
  4. wrapText(String inString, String newline, int wrapColumn)
  5. wrapText(String line, int maxLineLength)
  6. wrapText(String text, int len, boolean wordWrap)
  7. wrapText(String txt, char breakChar, String lineDelim, int numChars)