truncate string and show dots - Android java.lang

Android examples for java.lang:String Shorten

Description

truncate string and show dots

Demo Code

import android.util.Log;

public class Main{

    public static String substring(String str, int toCount) {
        return substring(str, toCount, "..");
    }/*from w w w  . j ava 2 s.  c om*/

    public static String substring(String str, int len, String more) {
        if (str == null || "".equals(str) || len < 1) {
            return "";
        }
        char[] chars = str.toCharArray();
        int count = 0;
        int charIndex = 0;
        for (int i = 0; i < chars.length; i++) {
            int charLength = getCharLen(chars[i]);
            if (count <= len - charLength) {
                count += charLength;
                charIndex++;
            } else {
                break;
            }
        }
        if (charIndex == chars.length) {
            return new String(chars, 0, charIndex);
        } else {
            return new String(chars, 0, charIndex) + more;
        }
    }

}

Related Tutorials