force Ellipsize End to shorten a string - Android java.lang

Android examples for java.lang:String Shorten

Description

force Ellipsize End to shorten a string

Demo Code


public class Main{

    public static String forceEllipsizeEnd(String str, int size) {
        if (str.length() > size) {
            return str.substring(0, size) + "...";
        }// w  ww. j a va2  s . c om
        return str;
    }
    public static String forceEllipsizeEnd(String str, float maxWidth,
            float singleWith) {
        String result = str.trim();
        int maxCount = (int) Math.floor(maxWidth / singleWith);
        if (result.length() > maxCount) {
            return forceEllipsizeEnd(result, maxCount - 1);
        } else {
            return result;
        }
    }

}

Related Tutorials