Here you can find the source of abbreviate(String str, int maximumChars)
Abbreviates the specified String
if its length exceeds the value specified by maximumChars
.
Parameter | Description |
---|---|
str | the <code>String</code> to abbreviate |
maximumChars | the maximum characters to allow in the specified input <code>String</code> |
String
public static String abbreviate(String str, int maximumChars)
//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); } }