Java String Truncate truncate(final String value, final int width)

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

Description

Truncate a string if the desired with is less than the length of the input string.

License

Open Source License

Parameter

Parameter Description
value String to pad
width Total width of output string

Declaration

public static String truncate(final String value, final int width) 

Method Source Code

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

public class Main {
    /**/*from  w  w w  .java 2s. com*/
     * Truncate a string if the desired with is less than the length of the input string.
     *
     * @param value String to pad
     * @param width Total width of output string
     * @return
     */
    public static String truncate(final String value, final int width) {
        if (width >= value.length()) {
            return value;
        }

        return value.substring(0, width - 3) + "...";
    }
}

Related

  1. truncate(final String s, final int n)
  2. truncate(final String str, final int len)
  3. truncate(final String str, final int len)
  4. truncate(final String str, final int maxWidth)
  5. truncate(final String target, final int maxSize)
  6. truncate(Object truncateMe, int maxLength, String suffix)
  7. truncate(String eval, String suffix, int targetLength)
  8. truncate(String input, int maxLength)
  9. truncate(String input, int maxLength)