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;
/**/*ww w .  j a  v a2 s .co m*/
* Converts a line of text into an array of lower case words using a
* BreakIterator.wordInstance().
* <p>
* 
* This method is under the Jive Open Source Software License and was written
* by Mark Imbriaco.
* 
* @param text
*          a String of text to convert into an array of words
* @return text broken up into an array of words.
*/

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) 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) {
        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 + 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);
        }

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

    public static final int length(String baseString) {
        if (baseString == null)
            return 0;
        else
            return baseString.length();
    }

    public static final String substring(String baseString, int start, int end) {
        if (baseString == null)
            return null;
        else if (start >= baseString.length() || start < 0 || end < 0 || start > end)
            return null;
        else if (end >= baseString.length())
            return baseString.substring(start);
        else {
            return baseString.substring(start, end);
        }
    }

    public static final String substring(String baseString, int start) {
        if (baseString == null)
            return null;
        else
            return substring(baseString, start, baseString.length());
    }

    /**
    * Trim
    */
    public static String trim(String baseString) {
        if (baseString == null)
            return null;
        else
            return baseString.trim();
    }
}

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)
  5. chopAtWordsAround(String input, String[] wordList, int numChars)