Java String Truncate truncate(String str, int n)

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

Description

Truncates a string and appends an ellipsis.

License

Open Source License

Parameter

Parameter Description
str String.
n Number of characters to retain.

Return

String truncated to n characters if necessary, with an ellipsis replacing the truncated characters.

Declaration


public static String truncate(String str, int n) 

Method Source Code

//package com.java2s;
/*   Please see the license information at the end of this file. */

public class Main {
    /**   Truncates a string and appends an ellipsis.
     *// w ww  .  j  av  a 2s  .  c  o  m
     *   @param   str      String.
     *
     *   @param   n      Number of characters to retain.
     *
     *   @return         String truncated to n characters if necessary, with an
     *               ellipsis replacing the truncated characters.
     */

    public static String truncate(String str, int n) {
        return str.length() < n ? str : (str.substring(0, n) + "...");
    }
}

Related

  1. truncate(String str, int max)
  2. truncate(String str, int max)
  3. truncate(String str, int maxLen)
  4. truncate(String str, int maxSize)
  5. truncate(String str, int maxSize)
  6. truncate(String str, int size, boolean useEllipsis)
  7. truncate(String str, int start, int end)
  8. truncate(String str, String prefix, String suffix)
  9. truncate(String string, final int length)