Java String Truncate truncate(String s, int maxLength)

Here you can find the source of truncate(String s, int maxLength)

Description

Truncate the specified String if it is longer than maxLength.

License

Open Source License

Declaration

public static String truncate(String s, int maxLength) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w ww . j a va2  s .  c  o  m*/
     * Truncate the specified String if it is longer than maxLength.
     */
    public static String truncate(String s, int maxLength) {
        return truncate(s, maxLength, "");
    }

    /**
     * Truncate the specified String if it is longer than maxLength.  The string will be truncated
     * at a position such that it is maxLength chars long after the addition of the 'append'
     * String.
     *
     * @param append a String to add to the truncated String only after truncation.
     */
    public static String truncate(String s, int maxLength, String append) {
        if ((s == null) || (s.length() <= maxLength)) {
            return s;
        } else {
            return s.substring(0, maxLength - append.length()) + append;
        }
    }
}

Related

  1. truncate(String s, int length)
  2. truncate(String s, int length)
  3. truncate(String s, int length, boolean indicator)
  4. truncate(String s, int max_len)
  5. truncate(String s, int maxLength)
  6. truncate(String s, int maxLength, boolean alignRight)
  7. truncate(String s, int stop, String suf)
  8. truncate(String s, int truncLength, boolean removeWhiteSpace)
  9. truncate(String source, int maxLength, String cutoffReplacement)