Here you can find the source of truncateString(String str, int length, String suffix)
Parameter | Description |
---|---|
string | the string |
length | the max length |
suffix | the suffix to add if the string is > length |
public static String truncateString(String str, int length, String suffix)
//package com.java2s; //License from project: Open Source License public class Main { /**//w ww .ja va 2 s .co m * Truncate a string by the given length, cutting words if necessary. * * @param string * the string * @param length * the max length * @param suffix * the suffix to add if the string is > length * @return the new string < length */ public static String truncateString(String str, int length, String suffix) { if (str != null && length >= 0) { suffix = (suffix == null ? "" : suffix); if (str.length() > (length - suffix.length())) { int size = length - suffix.length(); while (size < 0) { size++; } str = str.substring(0, size) + suffix; } } return (str == null ? null : str); } }