Java String Abbreviate abbreviate(String str, int maxWidth)

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

Description

Abbreviates a String using ellipses.

License

Open Source License

Parameter

Parameter Description
str the String to abbreviate
maxWidth maximum length of result String, must be at least 4

Exception

Parameter Description
IllegalArgumentException when the width is too small

Return

the abbreviated String

Declaration

public static String abbreviate(String str, int maxWidth) 

Method Source Code

//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) + "...";
    }
}

Related

  1. abbreviate(String str, int maxlen, String appendIfLimit)
  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 maxWidth)
  9. abbreviate(String str, int offset, int maxWidth)