Java String Truncate truncate(String text, int length)

Here you can find the source of truncate(String text, int length)

Description

Truncates the string to a particular length if it's longer and appends ...

License

Open Source License

Parameter

Parameter Description
text the string to be truncated
length the length to truncate to

Return

the truncated string

Declaration

public static String truncate(String text, int length) 

Method Source Code

//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$
    }
}

Related

  1. truncate(String string, int maxSize)
  2. truncate(String string, int n, String suffix)
  3. truncate(String strOrginal, int iByteMaxSize)
  4. truncate(String text)
  5. truncate(String text, int len)
  6. truncate(String text, int length)
  7. truncate(String text, int maxLength)
  8. truncate(String text, int size, String charsetName)
  9. truncate(String text, int truncatedLength)