Cuts the string at the end if it's longer than maxLength and appends "..." to it - Android java.lang

Android examples for java.lang:String Shorten

Description

Cuts the string at the end if it's longer than maxLength and appends "..." to it

Demo Code

public class Main{

    /**//from  w  w w. j av a  2 s  .co m
     * Cuts the string at the end if it's longer than maxLength and appends "..." to it. The length of the resulting
     * string including "..." is always less or equal to the given maxLength. It's valid to pass a null text; in this
     * case null is returned.
     */
    public static String ellipsize(String text, int maxLength) {
        if (text != null && text.length() > maxLength) {
            return text.substring(0, maxLength - 3) + "...";
        }
        return text;
    }

}

Related Tutorials