Java String Shorten shortenString(String source, int minLength, int maxLength, String suffix)

Here you can find the source of shortenString(String source, int minLength, int maxLength, String suffix)

Description

Shorten the string to the specifed lenght.

License

Open Source License

Parameter

Parameter Description
source the string to process.
minLength minimum length of the shortened string.
maxLength maximum length limit.
suffix suffix to add if the string is actually shortened. Should be " ..." or " \u2026" (using Unicode horizontal ellipsis glyph).

Return

shortened string.

Declaration

public static String shortenString(String source, int minLength, int maxLength, String suffix) 

Method Source Code

//package com.java2s;

public class Main {
    /**//w  w w.ja  v  a2 s. co m
     * Shorten the string to the specifed lenght.
     * 
     * <p>If the string is shorter than the maxLength limit it is returned intact. If the string is 
     * longer,it will be truncated and the suffix will be added. If truncation is necessary, the 
     * preferred truncation point is the last punctuation character before the limit, unless it
     * occurs later than minLength in the string. In this case the prefferred truncation point
     * is the last whitespace before the maxLength limit unless it occurs later than minLength.
     * At last resort, the string is truncated at maxLenght limit.</p>
     * 
     * @param source the string to process.
     * @param minLength minimum length of the shortened string.
     * @param maxLength maximum length limit.
     * @param suffix suffix to add if the string is actually shortened. Should be " ..." or
     * " \u2026" (using Unicode horizontal ellipsis glyph).
     * @return shortened string.
     */
    public static String shortenString(String source, int minLength, int maxLength, String suffix) {
        if (source == null || maxLength >= source.length()) {
            return source;
        }
        // try to find a punctuation character before length limit
        int i;
        for (i = maxLength - 1; i >= 0; i--) {
            int type = Character.getType(source.charAt(i));
            if (type >= Character.DASH_PUNCTUATION && type <= Character.OTHER_PUNCTUATION) {
                break;
            }
        }
        if (i < minLength) {
            // try to find a whitespace chracter before length limit
            for (i = maxLength - 1; i >= 0; i--) {
                if (Character.isWhitespace(source.charAt(i))) {
                    break;
                }
            }
        }
        if (i < minLength) {
            i = maxLength;
        }
        return source.substring(0, i) + suffix;
    }
}

Related

  1. shortenRemoteRef(final String origin, final String ref)
  2. shortenString(String orig, int charsToRemove)
  3. shortenString(String orig, int maxLength)
  4. shortenString(String s, int maxLength)
  5. shortenString(String s, int requiredLength)
  6. shortenString(String str, int i)
  7. shortenString(String string, int minimumLength, int lengthToShortenBy)
  8. shortenString(String string, int targetLength, int maxDeviation)
  9. shortenStringForDisplay(String str, int desiredLen)