Here you can find the source of truncateAtWord(String input, int length)
Parameter | Description |
---|---|
input | a parameter |
length | character limit |
public static String truncateAtWord(String input, int length)
//package com.java2s; //License from project: Open Source License public class Main { /**// w ww .j av a 2s . c o m * Returns first part of string upto specified length (breaks at word, greedy) * * @param input * @param length character limit * @return */ public static String truncateAtWord(String input, int length) { int offset; offset = 2; if (input == null || input.length() < (length - offset)) { return input; } int iNextSpace; iNextSpace = input.lastIndexOf(" ", length); String trunc = input; try { trunc = String.format(input.substring(0, (iNextSpace > 0) ? iNextSpace : (length - offset)).trim()); return trunc + " ..."; } catch (Exception e) { return trunc; } } }