Here you can find the source of truncate(String value, int length)
Parameter | Description |
---|---|
value | String to be truncated |
length | Maximum length of string |
public static String truncate(String value, int length)
//package com.java2s; //License from project: Open Source License public class Main { private static final String DOTS = "..."; /**/*from w w w . j a va 2 s . c o m*/ * Truncates a String to the given length. * * @param value * String to be truncated * @param length * Maximum length of string * @return Returns value if value is null or value.length() is less or equal * to length, otherwise a String representing value truncated to * length with added dots (...) */ public static String truncate(String value, int length) { if (value != null && value.length() > length) { value = value.substring(0, length); value += DOTS; } return value; } }