Here you can find the source of abbreviate(String str, int maxWidth)
Parameter | Description |
---|---|
str | the String to abbreviate. |
maxWidth | the maximum size of the string, minus the ellipsis. |
public static String abbreviate(String str, int maxWidth)
//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) + "..."; } }