Java String Abbreviate abbreviate(String str, int maximumChars)

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

Description

Abbreviates the specified String if its length exceeds the value specified by maximumChars.

License

Open Source License

Parameter

Parameter Description
str the <code>String</code> to abbreviate
maximumChars the maximum characters to allow in the specified input <code>String</code>

Return

the abbreviated String

Declaration

public static String abbreviate(String str, int maximumChars) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright notice                                                            *
 *                                                                             *
 * Copyright (c) 2005 Feed'n Read Development Team                             *
 * http://sourceforge.net/fnr                                                  *
 *                                                                             *
 * All rights reserved.                                                        *
 *                                                                             *
 * This program and the accompanying materials are made available under the    *
 * terms of the Common Public License v1.0 which accompanies this distribution,*
 * and is available at                                                         *
 * http://www.eclipse.org/legal/cpl-v10.html                                   *
 *                                                                             *
 * A copy is found in the file cpl-v10.html and important notices to the       *
 * license from the team is found in the textfile LICENSE.txt distributed      *
 * in this package.                                                            *
 *                                                                             *
 * This copyright notice MUST APPEAR in all copies of the file.                *
 *                                                                             *
 * Contributors:                                                               *
 *    Feed'n Read - initial API and implementation                             *
 *                  (smachhau@users.sourceforge.net)                           *
 *******************************************************************************/

public class Main {
    /**//  w  w w  .j a  va  2 s.c  om
     * <p>
     * Abbreviates the specified <code>String</code> if its length exceeds the
     * value specified by <code>maximumChars</code>. The resulting
     * <code>String</code> will then be a substring of <code>str</code>
     * beginning at index 0 and ending at index <code>maximumChars</code>-1
     * plus a concatenated "..." to show that an abbreviation has been done. If
     * the length of <code>str</code> is below the value specified by
     * <code>maximumChars</code> the input <code>String</code> remains
     * untouched.
     * </p>
     * 
     * @param str the <code>String</code> to abbreviate
     * @param maximumChars the maximum characters to allow in the specified
     * input <code>String</code>
     * 
     * @return the abbreviated <code>String</code>
     */
    public static String abbreviate(String str, int maximumChars) {
        if (!isEmpty(str)) {
            if (str.length() > maximumChars) {
                return (str.substring(0, maximumChars) + "...");
            }
        }
        return (str);
    }

    /**
     * <p>
     * Tests if the given <code>String</code> is empty. A <code>String</code>
     * object is considered empty if it is either <code>null</code> or its
     * trimmed length is zero.
     * </p>
     * 
     * @param str the <code>String</code> to test
     * @return <code>true</code> if the given <code>String</code> is empty;
     * <code>false</code> otherwise
     */
    public static boolean isEmpty(String str) {
        return (str == null || str.trim().length() == 0);
    }
}

Related

  1. abbreviate(String s, int length)
  2. abbreviate(String s, int max)
  3. abbreviate(String s, int maxLength)
  4. abbreviate(String src, int maxlen, String replacement)
  5. abbreviate(String str)
  6. abbreviate(String str, int maxlen, String appendIfLimit)
  7. abbreviate(String str, int maxWidth)
  8. abbreviate(String str, int maxWidth)
  9. abbreviate(String str, int maxWidth)