add Ellipsis to string - Android java.lang

Android examples for java.lang:String Shorten

Description

add Ellipsis to string

Demo Code

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static String addEllipsis(String src, String ellipsis,
            int maxLength) {
        if (maxLength < 0) {
            throw new IllegalArgumentException(
                    "maxLength could not less than zero");
        }/*from  w ww .  ja  v  a 2s  .  co  m*/
        int srcLength = src.length();
        if (srcLength <= maxLength) {
            return src;
        }
        int ellipsisLength = ellipsis.length();
        int dis = srcLength + ellipsisLength - maxLength;
        if (dis < srcLength) {
            return src.substring(0, srcLength - dis) + ellipsis;
        }
        return ellipsis.substring(dis - srcLength);
    }

}

Related Tutorials