Java String Truncate truncateAtWhitespace(String text, int length)

Here you can find the source of truncateAtWhitespace(String text, int length)

Description

Truncate text on a whitespace boundary (near a specified length).

License

Educational Community License

Parameter

Parameter Description
text Text to truncate
length Target length

Return

Truncated text

Declaration

public static String truncateAtWhitespace(String text, int length) 

Method Source Code

//package com.java2s;
/**********************************************************************************
 *
 * Copyright (c) 2003, 2004, 2007, 2008 The Sakai Foundation
 *
 * Licensed under the Educational Community 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.opensource.org/licenses/ECL-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.//from   w w w .  j  av  a 2  s. com
 *
 **********************************************************************************/

public class Main {
    /**
     * Minimum length supported by <code>truncateAtWhitespace()</code>
     */
    private static final int MINIMUM_SUPPORTED_LENGTH = 4;

    /**
     * Truncate text on a whitespace boundary (near a specified length).
     * The length of the resultant string will be in the range:<br>
     * <code>   (requested-length * .25) ~ (requested-length * 1.5) </code>
     * @param text Text to truncate
     * @param length Target length
     * @return Truncated text
     */
    public static String truncateAtWhitespace(String text, int length) {
        int desired, lowerBound, upperBound;
        /*
         * Make sure we have a reasonable length to work with
         */
        if (length < MINIMUM_SUPPORTED_LENGTH) {
            throw new IllegalArgumentException(
                    "Requested length too short (must be "
                            + MINIMUM_SUPPORTED_LENGTH + " or greated)");
        }
        /*
         * No need to truncate - the original string "fits"
         */
        if (text.length() <= length) {
            return text;
        }
        /*
         * Try to find whitespace befor the requested maximum
         */
        lowerBound = length / 4;
        upperBound = length + (length / 2);

        for (int i = length - 1; i > lowerBound; i--) {
            if (Character.isWhitespace(text.charAt(i))) {
                return text.substring(0, i);
            }
        }
        /*
         * No whitespace - look beyond the desired maximum
         */
        for (int i = (length); i < upperBound; i++) {
            if (Character.isWhitespace(text.charAt(i))) {
                return text.substring(0, i);
            }
        }
        /*
         * No whitespace, just truncate the text at the requested length
         */
        return text.substring(0, length);
    }
}

Related

  1. truncateAndTrailOffText(String text, int limit)
  2. truncateAndTrim0(String str, String delim)
  3. truncateAt(final String input, final String... substrings)
  4. truncateAt(String str, char chr)
  5. truncateAtMaxLength(String source, int maxLength, boolean addEllipsis)
  6. truncateAtWord(String input, int length)
  7. truncateBodyText(String input)
  8. truncateByCharLength(String string, int maxLength)
  9. truncateByCharLengthWithEllipsis(String string, int maxLength)