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

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

Description

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

License

LGPL

Parameter

Parameter Description
string the String to chop.
length the index in <code>string</code> to start looking for a whitespace boundary at.

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) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from w  w  w .  j a v  a  2  s.  co m*/
     * 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.
     * </p>
     *
     * @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) {
        if (string == null) {
            return string;
        }

        char[] charArray = string.toCharArray();
        int sLength = string.length();

        if (length < sLength) {
            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);
            } //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);
        }

        //Done checking for newline, now see if the total string is less than
        //the specified chop point.
        if (string.length() < length) {
            return string;
        }

        //No newline, so chop at the first whitespace.
        for (int i = length - 1; i > 0; i--) {
            if (charArray[i] == ' ') {
                return string.substring(0, i).trim();
            }
        }

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

Related

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