Here you can find the source of truncate(String str, int length)
Parameter | Description |
---|---|
str | The string to truncate. |
length | The maximum length of the desired string, which must be at least three. |
public static String truncate(String str, int length)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from ww w. j ava 2 s. c om*/ * Truncates the given string so it is no longer * than the given length, which must be at least * three. * @param str The string to truncate. * @param length The maximum length of the desired * string, which must be at least three. * @return The given string, truncated to at most * length characters, with "..." at its end if * it is truncated. */ public static String truncate(String str, int length) { assert length >= 3 : length; length = length - 3; if (str.length() <= length) return str; else return str.substring(0, length) + "..."; } }