Java String Truncate truncate(String value, int length)

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

Description

Truncates a String to the given length.

License

Open Source License

Parameter

Parameter Description
value String to be truncated
length Maximum length of string

Return

Returns value if value is null or value.length() is less or equal to length, otherwise a String representing value truncated to length with added dots (...)

Declaration

public static String truncate(String value, int length) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static final String DOTS = "...";

    /**/*from  w w w  .  j a va  2 s .  c  o m*/
     * Truncates a String to the given length.
     *
     * @param value
     *            String to be truncated
     * @param length
     *            Maximum length of string
     * @return Returns value if value is null or value.length() is less or equal
     *         to length, otherwise a String representing value truncated to
     *         length with added dots (...)
     */
    public static String truncate(String value, int length) {
        if (value != null && value.length() > length) {
            value = value.substring(0, length);
            value += DOTS;
        }
        return value;
    }
}

Related

  1. truncate(String toTruncate, int maxSize)
  2. truncate(String url, int offset, int size)
  3. Truncate(String v, int length)
  4. truncate(String val, int maxlen)
  5. truncate(String value, int length)
  6. truncate(String value, int length)
  7. truncate(String value, int length)
  8. truncate(String value, int maxLen, String ellipses)
  9. truncate(String x, int length)