Here you can find the source of ellipsize(String text, int max)
From <a href="http://stackoverflow.com/a/3657496/3802890">StackOverflow</a>
public static String ellipsize(String text, int max)
//package com.java2s; //License from project: Apache License public class Main { /**//from w ww . j av a 2 s .c o m * From <a href="http://stackoverflow.com/a/3657496/3802890">StackOverflow</a> */ public static String ellipsize(String text, int max) { if (textWidth(text) <= max) return text; // Start by chopping off at the word before max // This is an over-approximation due to thin-characters... int end = text.lastIndexOf(' ', max - 3); // Just one long word. Chop it off. if (end == -1) return text.substring(0, max - 3) + "..."; // Step forward as long as textWidth allows. int newEnd = end; do { end = newEnd; newEnd = text.indexOf(' ', end + 1); // No more spaces. if (newEnd == -1) newEnd = text.length(); } while (textWidth(text.substring(0, newEnd) + "...") < max); return text.substring(0, end) + "..."; } private static int textWidth(String str) { return str.length() - str.replaceAll("[^iIl1\\.,']", "").length() / 2; } }