Here you can find the source of truncate(String s, int maxLength)
public static String truncate(String s, int maxLength)
//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; } } }