ellipsize String - Android java.lang

Android examples for java.lang:String Shorten

Description

ellipsize String

Demo Code

import android.content.Context;
import android.content.res.AssetManager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main{

    /**/*from   w ww . j a v  a2 s  .  c o m*/
     * ellipsize a string
     * @param s string to ellipsize...
     * @param maxlenSpaces maximum length with spaces (not really, we start here)
     * @param maxlenNoSpaces maxmium length,really
     * @return ellipsized string...
     */
    public static String ellipsizeString(String s, int maxlenSpaces,
            int maxlenNoSpaces) {
        if (s.length() < maxlenNoSpaces) {
            return s;
        }
        for (int ich = maxlenSpaces; ich < maxlenNoSpaces; ich++) {
            char ch = s.charAt(ich);
            if (Character.isWhitespace(ch) || (ch == '\n')) {
                return s.substring(0, ich) + "...";
            }
        }
        return s.substring(0, maxlenNoSpaces) + "...";
    }

}

Related Tutorials