Here you can find the source of truncate(String str, int n)
Parameter | Description |
---|---|
str | String. |
n | Number of characters to retain. |
public static String truncate(String str, int n)
//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) + "..."); } }