Java String Truncate truncateString(String description, int length)

Here you can find the source of truncateString(String description, int length)

Description

Will truncated the string to 35 characters in length adding an ellipsis for the last three characters.

License

Open Source License

Parameter

Parameter Description
description a parameter

Declaration

public static String truncateString(String description, int length) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006, 2007 Bug Labs, Inc..
 * 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.buglabs.net/legal/epl_license.html
 *******************************************************************************/

public class Main {
    /**//from  w w  w. ja  v  a2  s.c o m
     * Will truncated the string to 35 characters in length adding an ellipsis
     * for the last three characters.
     * 
     * @param description
     * @return
     */
    public static String truncateString(String description, int length) {
        String truncated = "";
        if (description.length() > length) {
            truncated = description.substring(0, length - 3);
            truncated = truncated + "...";
        } else {
            truncated = description;
        }

        return truncated;
    }
}

Related

  1. truncateSentence(String input, int length)
  2. truncateShortMessage(final String message)
  3. truncateStr(String str, int maxNumChars)
  4. truncateString(final String str, final int iMaxLength)
  5. truncateString(int length, String srcString)
  6. truncateString(String errCode, int length, boolean isRight)
  7. truncateString(String inStr, int maxValue)
  8. truncateString(String s)
  9. truncateString(String s, int numChars)