Java String Truncate truncate(final String str, final int len)

Here you can find the source of truncate(final String str, final int len)

Description

truncate a string if it longer than the argument

License

Open Source License

Parameter

Parameter Description
str String to truncate
len maximum desired length of new string

Declaration

public static String truncate(final String str, final int len) 

Method Source Code

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

public class Main {
    /**//from ww  w.j  a v a 2s.co  m
     * truncate a string if it longer than the argument
     *
     * @param str String to truncate
     * @param len maximum desired length of new string
     */
    public static String truncate(final String str, final int len) {
        if (str == null) {
            throw new NullPointerException("String is null");
        }
        if (len < 0) {
            throw new IndexOutOfBoundsException("Length is less than zero");
        }
        if (str.length() > len) {
            return str.substring(0, len);
        }
        return str.trim();
    }
}

Related

  1. truncate(final String description)
  2. truncate(final String in, final int maxLen)
  3. truncate(final String originalString, final int maxChars)
  4. truncate(final String s, final int n)
  5. truncate(final String str, final int len)
  6. truncate(final String str, final int maxWidth)
  7. truncate(final String target, final int maxSize)
  8. truncate(final String value, final int width)
  9. truncate(Object truncateMe, int maxLength, String suffix)