Here you can find the source of abbreviate(String str, int maxWidth)
public static String abbreviate(String str, int maxWidth)
//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()); } }