Abbreviates a string nicely. - Android java.lang

Android examples for java.lang:String Shorten

Description

Abbreviates a string nicely.

Demo Code

/*******************************************************************************
 * Copyright (c) 2011 MadRobot./*from   www .j a  v a2s. co  m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *  Elton Kent - initial API and implementation
 ******************************************************************************/
import java.util.ArrayList;
import android.graphics.Paint;

public class Main{
    /**
     * Abbreviates a string nicely.
     * 
     * This method searches for the first space after the lower limit and
     * abbreviates the String there. It will also append any String passed as a
     * parameter to the end of the String. The upper limit can be specified to
     * forcibly abbreviate a String.
     * 
     * @param str
     *            the string to be abbreviated. If null is passed, null is
     *            returned. If the empty String is passed, the empty string is
     *            returned.
     * @param lower
     *            the lower limit.
     * @param upper
     *            the upper limit; specify -1 if no limit is desired. If the
     *            upper limit is lower than the lower limit, it will be adjusted
     *            to be the same as the lower limit.
     * @param appendToEnd
     *            String to be appended to the end of the abbreviated string.
     *            This is appended ONLY if the string was indeed abbreviated.
     *            The append does not count towards the lower or upper limits.
     * @return the abbreviated String.
     * @since 2.4
     */
    public static String abbreviate(String str, int lower, int upper,
            String appendToEnd) {
        // initial parameter checks
        if (str == null) {
            return null;
        }
        if (str.length() == 0) {
            return StringUtils.EMPTY;
        }

        // if the lower value is greater than the length of the string,
        // set to the length of the string
        if (lower > str.length()) {
            lower = str.length();
        }
        // if the upper value is -1 (i.e. no limit) or is greater
        // than the length of the string, set to the length of the string
        if (upper == -1 || upper > str.length()) {
            upper = str.length();
        }
        // if upper is less than lower, raise it to lower
        if (upper < lower) {
            upper = lower;
        }

        StringBuilder result = new StringBuilder();
        int index = StringUtils.indexOf(str, " ", lower);
        if (index == -1) {
            result.append(str.substring(0, upper));
            // only if abbreviation has occured do we append the appendToEnd
            // value
            if (upper != str.length()) {
                result.append(StringUtils.defaultString(appendToEnd));
            }
        } else if (index > upper) {
            result.append(str.substring(0, upper));
            result.append(StringUtils.defaultString(appendToEnd));
        } else {
            result.append(str.substring(0, index));
            result.append(StringUtils.defaultString(appendToEnd));
        }
        return result.toString();
    }
}

Related Tutorials