Here you can find the source of truncate(String text, int length)
Parameter | Description |
---|---|
text | the string to be truncated |
length | the length to truncate to |
public static String truncate(String text, int length)
//package com.java2s; /**/*w ww . j a v a 2s .c o m*/ * Aptana Studio * Copyright (c) 2005-2012 by Appcelerator, Inc. All Rights Reserved. * Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions). * Please see the license.html included with this distribution for details. * Any modifications to this file must keep this entire header intact. */ public class Main { /** * Truncates the string to a particular length if it's longer and appends ... in the end. * * @param text * the string to be truncated * @param length * the length to truncate to * @return the truncated string */ public static String truncate(String text, int length) { if (text == null || text.length() <= length) { return text; } return new String(ellipsify(text.substring(0, length))); } /** * Adds an ellipsis to the end of a string, generally indicating that this string leads to another choice (like a * dialog) * * @param message * @return The ellipsif-ied string */ public static String ellipsify(String message) { return message == null ? null : message + "..."; //$NON-NLS-1$ } }