Write code to ellipsize String
Check null value, check the specified length and string length
Use String.substring
//package com.book2s; public class Main { public static void main(String[] argv) { String string = "book2s.com"; int limit = 4; System.out.println(ellipsizeString(string, limit)); }/* ww w .j av a 2s .c om*/ public static String ellipsizeString(String string, int limit) { String retValue = ""; if (string != null) { if (string.length() > limit) { retValue = string.substring(0, limit) + "..."; } else { retValue = string; } } return retValue; } }