Android String Shorten addEllipsis(String src, String ellipsis, int maxLength)

Here you can find the source of addEllipsis(String src, String ellipsis, int maxLength)

Description

add Ellipsis

Declaration

public static String addEllipsis(String src, String ellipsis,
            int maxLength) 

Method Source Code

//package com.java2s;

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  w w.  j av a  2  s. 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

  1. toLength(String str, int length)
  2. truncate(String text, int length)
  3. truncateAt(String string, int length)
  4. curtail(String str, int size, String tail)