Here you can find the source of shortenText(int maxWidth, String textValue)
t
so that its length doesn't exceed the given width.
Parameter | Description |
---|---|
maxWidth | the maximum length for the text |
textValue | the text to be shortened |
public static String shortenText(int maxWidth, String textValue)
//package com.java2s; /******************************************************************************* * Copyright (c) 2000, 2012 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:/*w w w. ja v a 2 s.c o m*/ * IBM Corporation - initial API and implementation *******************************************************************************/ public class Main { /** * Shorten the given text <code>t</code> so that its length * doesn't exceed the given width. This implementation * replaces characters in the center of the original string with an * ellipsis ("..."). * @param maxWidth the maximum length for the text * @param textValue the text to be shortened * @return the shortened text */ public static String shortenText(int maxWidth, String textValue) { int length = textValue.length(); if (length < maxWidth) return textValue; String ellipsis = "..."; //$NON-NLS-1$ int subStrLen = (maxWidth - ellipsis.length()) / 2; int addtl = (maxWidth - ellipsis.length()) % 2; StringBuffer sb = new StringBuffer(); sb.append(textValue.substring(0, subStrLen)); sb.append(ellipsis); sb.append(textValue.substring(length - subStrLen - addtl)); return sb.toString(); } }