Java String Abbreviate abbreviate(String str, int maxWidth)

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

Description

abbreviate

License

Open Source License

Declaration

public static String abbreviate(String str, int maxWidth) 

Method Source Code

//package com.java2s;
/**/*w ww  . j a  v  a2  s.co m*/
 *  Copyright (C) 2012 White Source (www.whitesourcesoftware.com)
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This patch 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 Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this patch.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static String abbreviate(String str, int maxWidth) {
        if (isBlank(str)) {
            return str;
        }
        if (str.length() <= maxWidth) {
            return str;
        }
        if (maxWidth < 3) {
            throw new IllegalArgumentException("maxWidth must be >= 3");
        }
        return str.substring(0, maxWidth - 3) + "...";
    }

    public static boolean isBlank(String str) {
        return str == null || "".equals(str.trim());
    }
}

Related

  1. abbreviate(String str, int maxWidth)
  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 offset, int maxWidth)
  7. abbreviate(String str, int preferredLength)
  8. abbreviate(String string, int maxLength)
  9. abbreviate(String text, int maxFront, int maxBack)