Java String Shorten shortenStringIfNecessary(String string, int maxLength, String suffixToAppend)

Here you can find the source of shortenStringIfNecessary(String string, int maxLength, String suffixToAppend)

Description

shorten String If Necessary

License

Open Source License

Declaration

public static String shortenStringIfNecessary(String string, int maxLength, String suffixToAppend) 

Method Source Code

//package com.java2s;

public class Main {
    public static final String ELLIPSIS = "...";

    public static String shortenStringIfNecessary(String string, int maxLength, String suffixToAppend) {
        if (string == null)
            return null; // FIXME: throw IllegalState or log a warning ?
        return (string.length() > maxLength) ? string.substring(0, maxLength) + suffixToAppend : string;
    }/* w w  w.ja va  2 s. co  m*/

    public static String shortenStringIfNecessary(String string, int maxLength) {
        return shortenStringIfNecessary(string, maxLength, ELLIPSIS);
    }
}

Related

  1. shortenString(String source, int minLength, int maxLength, String suffix)
  2. shortenString(String str, int i)
  3. shortenString(String string, int minimumLength, int lengthToShortenBy)
  4. shortenString(String string, int targetLength, int maxDeviation)
  5. shortenStringForDisplay(String str, int desiredLen)
  6. shortenStringsByRemovingVowelsToFit(String s1, String s2, int maximumStringLength)
  7. shortenTagString(String longTag)
  8. shortenText(final String text, final int maxLength, final boolean addDots)
  9. shortenText(int maxWidth, String textValue)