Here you can find the source of truncate(String s, int length)
Parameter | Description |
---|---|
s | The string to truncate |
length | The maximum length of the string |
public static String truncate(String s, int length)
//package com.java2s; // J2MEUnit is free software distributed under the Common Public License (CPL). public class Main { /*************************************** * Truncates a string to a maximum length. */*from w w w .jav a2 s.c o m*/ * @param s The string to truncate * @param length The maximum length of the string * * @return If the string is longer than length, the truncated string, else * the original string */ public static String truncate(String s, int length) { length -= 3; if (s.length() > length) s = s.substring(0, length) + "..."; return s; } }