Java String Chop at Word chopAtWord(String string, int length, int minLength)

Here you can find the source of chopAtWord(String string, int length, int minLength)

Description

Intelligently chops a String at a word boundary (whitespace) that occurs at the specified index in the argument or before.

License

Open Source License

Parameter

Parameter Description
string the String to chop.
length the index in <code>string</code> to start looking for a whitespace boundary at.
minLength the minimum length the word should be chopped at. This is helpful for words with no natural boundaries, ie: "thisisareallylonglonglongword". This must be smaller than length and can be -1 if no minLength is wanted

Return

a substring of string whose length is less than or equal to length, and that is chopped at whitespace.

Declaration

public static final String chopAtWord(String string, int length, int minLength) 

Method Source Code

//package com.java2s;
/**/*from   w  ww  . jav a  2 s  .  c o  m*/
 * $RCSFile$
 * $Revision: 18401 $
 * $Date: 2005-02-05 10:18:52 -0800 (Sat, 05 Feb 2005) $
 *
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
 */

public class Main {
    /**
     * Intelligently chops a String at a word boundary (whitespace) that occurs
     * at the specified index in the argument or before. However, if there is a
     * newline character before <code>length</code>, the String will be chopped
     * there. If no newline or whitespace is found in <code>string</code> up to
     * the index <code>length</code>, the String will chopped at <code>length</code>.
     * <p/>
     * For example, chopAtWord("This is a nice String", 10, -1) will return
     * "This is a" which is the first word boundary less than or equal to 10
     * characters into the original String.
     *
     * @param string    the String to chop.
     * @param length    the index in <code>string</code> to start looking for a
     *                  whitespace boundary at.
     * @param minLength the minimum length the word should be chopped at. This is helpful
     *                  for words with no natural boundaries, ie: "thisisareallylonglonglongword".
     *                  This must be smaller than length and can be -1 if no minLength is wanted
     * @return a substring of <code>string</code> whose length is less than or
     *         equal to <code>length</code>, and that is chopped at whitespace.
     */
    public static final String chopAtWord(String string, int length, int minLength) {
        // guard clauses
        if (length < 2) {
            throw new IllegalArgumentException("Length specified (" + length + ") must be > 2");
        } else if (minLength >= length) {
            throw new IllegalArgumentException("minLength must be smaller than length");
        }

        int sLength = (string == null) ? -1 : string.length();

        // shortcircuit clauses
        if (sLength < 1) {
            return string;
        }
        // minLength specified, string is smaller than the minLength, return the string
        else if (minLength != -1 && sLength < minLength) {
            return string;
        }
        // no minLength specified, string is smaller than length
        else if (minLength == -1 && sLength < length) {
            return string;
        }

        char[] charArray = string.toCharArray();

        // String is longer than the length specified, attempt to find a newline
        // or a space
        if (sLength > length) {
            sLength = length;

            // First check if there is a newline character before length; if so,
            // chop word there.
            for (int i = 0; i < sLength - 1; i++) {
                // Windows
                if (charArray[i] == '\r' && charArray[i + 1] == '\n') {
                    return string.substring(0, i + 1);
                }
                // Unix
                else if (charArray[i] == '\n') {
                    return string.substring(0, i);
                }
            }
            // Also check boundary case of Unix newline
            if (charArray[sLength - 1] == '\n') {
                return string.substring(0, sLength - 1);
            }

            // No newline, so chop at the first whitespace.
            for (int i = sLength - 1; i > 0; i--) {
                if (charArray[i] == ' ') {
                    return string.substring(0, i).trim();
                }
            }
        }
        // String is shorter than length but longer than minLength,
        // make sure there is a space in the string before minLength
        else if (minLength != -1 && sLength > minLength) {
            for (int i = 0; i < minLength; i++) {
                if (charArray[i] == ' ') {
                    return string;
                }
            }
        }

        // Did not find a word boundary, so return a string at the min length, if a min
        // length was specified:
        if (minLength > -1 && minLength <= string.length()) {
            return string.substring(0, minLength);
        }

        // Did not find word boundary or min length so return original String chopped at
        // specified length.
        return string.substring(0, length);
    }

    /**
     * Intelligently chops a String at a word boundary (whitespace) that occurs
     * at the specified index in the argument or before. However, if there is a
     * newline character before <code>length</code>, the String will be chopped
     * there. If no newline or whitespace is found in <code>string</code> up to
     * the index <code>length</code>, the String will chopped at <code>length</code>.
     * <p/>
     * For example, chopAtWord("This is a nice String", 10) will return
     * "This is a" which is the first word boundary less than or equal to 10
     * characters into the original String.
     *
     * @param string the String to chop.
     * @param length the index in <code>string</code> to start looking for a
     *               whitespace boundary at.
     * @return a substring of <code>string</code> whose length is less than or
     *         equal to <code>length</code>, and that is chopped at whitespace.
     */
    public static final String chopAtWord(String string, int length) {
        return chopAtWord(string, length, -1);
    }
}

Related

  1. chopAtWord(String string, int length)
  2. chopAtWord(String string, int length)
  3. chopAtWord(String string, int length, int minLength)
  4. chopAtWordsAround(String input, String wordList[], int numChars)
  5. chopAtWordsAround(String input, String[] wordList, int numChars)