Java String abbreviate at word
//package com.demo2s; public class Main { public static void main(String[] argv) throws Exception { String str = "this is a test test test"; int maxlen = 20; String elipses = ""; System.out.println(abbreviateAtWord(str, maxlen, elipses)); }//from www . j a v a 2 s . c o m public static String abbreviateAtWord(String str, int maxlen, String elipses) { if (str == null) { return ""; } if (str.length() > maxlen) { maxlen -= elipses.length(); int pos = str.lastIndexOf(' ', maxlen); if (pos == -1 || pos > maxlen) { str = ""; } else { str = str.substring(0, pos); if (elipses.isEmpty() == false) { str += elipses; } } } return str; } }