curtail string and display ... - Android java.lang

Android examples for java.lang:String Shorten

Description

curtail string and display ...

Demo Code

public class Main{

    /**//from w w w.j a  va  2  s  .c  o  m
     * <pre>
     * StringUtil.curtail(null, *, *) = null
     * StringUtil.curtail("abcdefghijklmnopqr", 10, null) = "abcdefghij"
     * StringUtil.curtail("abcdefghijklmnopqr", 10, "..") = "abcdefgh.."
     * </pre>
     * 
     */
    public static String curtail(String str, int size, String tail) {
        if (str == null) {
            return null;
        }
        int strLen = str.length();
        int tailLen = (tail != null) ? tail.length() : 0;
        int maxLen = size - tailLen;
        int curLen = 0;
        int index = 0;
        for (; index < strLen && curLen < maxLen; index++) {
            if (Character.getType(str.charAt(index)) == Character.OTHER_LETTER) {
                curLen++;
            }
            curLen++;
        }

        if (index == strLen) {
            return str;
        } else {
            StringBuilder result = new StringBuilder();
            result.append(str.substring(0, index));
            if (tail != null) {
                result.append(tail);
            }
            return result.toString();
        }
    }

}

Related Tutorials