Java String Truncate truncate(String string, int length)

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

Description

Truncates string at given length, appends "..." if string was shortened

License

Open Source License

Parameter

Parameter Description
string string to truncate
length length to truncate at

Return

if string length is less than or equal to given length then the given string, otherwise the given string truncated at given length with ... appended

Declaration

public static String truncate(String string, int length) 

Method Source Code

//package com.java2s;

public class Main {
    /**//  ww  w .j  a v a2  s. c  o  m
     * Truncates string at given length, appends <code>"..."</code> if string was shortened
     * @param string string to truncate
     * @param length length to truncate at
     * @return if string length is less than or equal to given length then the given string, otherwise the given string truncated at given length with {@code ...} appended
     */
    public static String truncate(String string, int length) {
        return string.length() > length ? string.substring(0, length) + "..." : string;
    }
}

Related

  1. truncate(String str, int n)
  2. truncate(String str, int size, boolean useEllipsis)
  3. truncate(String str, int start, int end)
  4. truncate(String str, String prefix, String suffix)
  5. truncate(String string, final int length)
  6. truncate(String string, int maxLength, boolean atBeginning, String ellipse)
  7. truncate(String string, int maxSize)
  8. truncate(String string, int n, String suffix)
  9. truncate(String strOrginal, int iByteMaxSize)