Java String Abbreviate abbreviate(String str, int maxWidth)

Here you can find the source of abbreviate(String str, int maxWidth)

Description

Abbreviates a string to a specified length and then adds an ellipsis if the input is greater than the maxWidth.

License

Open Source License

Parameter

Parameter Description
str the String to abbreviate.
maxWidth the maximum size of the string, minus the ellipsis.

Return

the abbreviated String, or null if the string was null.

Declaration

public static String abbreviate(String str, int maxWidth) 

Method Source Code

//package com.java2s;
/**//from  www . ja  v  a2 s .  c  om
 * <p>
 * Simple utility class for String operations useful across the framework.
 * <p/>
 * <p>
 * Some methods in this class were copied from the Spring Framework so we didn't
 * have to re-invent the wheel, and in these cases, we have retained all
 * license, copyright and author information.
 *
 * @since 0.9
 */

public class Main {
    /**
     * Abbreviates a string to a specified length and then adds an ellipsis if
     * the input is greater than the maxWidth. Example input:
     * <pre>
     *      user1@jivesoftware.com/home
     * </pre> and a maximum length of 20 characters, the abbreviate method will
     * return:
     * <pre>
     *      user1@jivesoftware.c...
     * </pre>
     *
     * @param str the String to abbreviate.
     * @param maxWidth the maximum size of the string, minus the ellipsis.
     * @return the abbreviated String, or <tt>null</tt> if the string was
     * <tt>null</tt>.
     */
    public static String abbreviate(String str, int maxWidth) {
        if (null == str) {
            return null;
        }

        if (str.length() <= maxWidth) {
            return str;
        }

        return str.substring(0, maxWidth) + "...";
    }
}

Related

  1. abbreviate(String str, int maxWidth)
  2. abbreviate(String str, int maxWidth)
  3. abbreviate(String str, int maxWidth)
  4. abbreviate(String str, int maxWidth)
  5. abbreviate(String str, int maxWidth)
  6. abbreviate(String str, int maxWidth)
  7. abbreviate(String str, int maxWidth)
  8. abbreviate(String str, int offset, int maxWidth)
  9. abbreviate(String str, int preferredLength)