Here you can find the source of abbreviate(String str, int maxWidth)
Parameter | Description |
---|---|
str | the String to abbreviate |
maxWidth | maximum length of result String, must be at least 4 |
Parameter | Description |
---|---|
IllegalArgumentException | when the width is too small |
public static String abbreviate(String str, int maxWidth)
//package com.java2s; /*/*www . java 2 s .c o m*/ * Smart GWT (GWT for SmartClient) * Copyright 2008 and beyond, Isomorphic Software, Inc. * * Smart GWT is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License version 3 * is published by the Free Software Foundation. Smart GWT is also * available under typical commercial license terms - see * http://smartclient.com/license * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. */ public class Main { /** * Abbreviates a String using ellipses. StringUtils.abbreviate("abcdefg", 6) = "abc..." * * @param str the String to abbreviate * @param maxWidth maximum length of result String, must be at least 4 * @return the abbreviated String * @throws IllegalArgumentException when the width is too small */ public static String abbreviate(String str, int maxWidth) { if (str == null) { return null; } if (str.length() < maxWidth) { return str; } if (maxWidth < 4) { throw new IllegalArgumentException("Minimum required width is 4"); } return str.substring(0, maxWidth - 3) + "..."; } }