Java String Truncate truncateAndTrailOffText(String text, int limit)

Here you can find the source of truncateAndTrailOffText(String text, int limit)

Description

truncate And Trail Off Text

License

Open Source License

Declaration

public static String truncateAndTrailOffText(String text, int limit) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  Copyright (c) 2006, 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
 * /*from w  w  w  . jav  a  2s .c  om*/
 *  Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

public class Main {
    public static final String F_DOTS = "...";

    public static String truncateAndTrailOffText(String text, int limit) {
        String trimmed = text.trim();
        int dotsLength = F_DOTS.length();
        int trimmedLength = trimmed.length();
        int limitWithDots = limit - dotsLength;

        if (limit >= trimmedLength) {
            return trimmed;
        }
        // limit <= trimmedLength
        if (limit <= dotsLength) {
            return ""; //$NON-NLS-1$
        }
        // dotsLength < limit < trimmedLength
        return trimmed.substring(0, limitWithDots) + F_DOTS;
    }
}

Related

  1. truncate(String value, int length)
  2. truncate(String value, int length)
  3. truncate(String value, int length)
  4. truncate(String value, int maxLen, String ellipses)
  5. truncate(String x, int length)
  6. truncateAndTrim0(String str, String delim)
  7. truncateAt(final String input, final String... substrings)
  8. truncateAt(String str, char chr)
  9. truncateAtMaxLength(String source, int maxLength, boolean addEllipsis)