Java String Truncate truncate(String s, int length)

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

Description

Truncates a string to a maximum length.

License

Common Public License

Parameter

Parameter Description
s The string to truncate
length The maximum length of the string

Return

If the string is longer than length, the truncated string, else the original string

Declaration

public static String truncate(String s, int length) 

Method Source Code

//package com.java2s;
// J2MEUnit is free software distributed under the Common Public License (CPL).

public class Main {
    /***************************************
     * Truncates a string to a maximum length.
     */*from   w  w w  .jav  a2  s.c  o  m*/
     * @param s The string to truncate
     * @param length The maximum length of the string
     *
     * @return If the string is longer than length, the truncated string, else
     *         the original string 
     */
    public static String truncate(String s, int length) {
        length -= 3;

        if (s.length() > length)
            s = s.substring(0, length) + "...";

        return s;
    }
}

Related

  1. truncate(String s)
  2. truncate(String s)
  3. truncate(String s, int begin, int end)
  4. truncate(String s, int i)
  5. truncate(String s, int length)
  6. truncate(String s, int length, boolean indicator)
  7. truncate(String s, int max_len)
  8. truncate(String s, int maxLength)
  9. truncate(String s, int maxLength)