Is it possible to truncate a Java string to the closest word boundary after a number of characters. Similar to the PHP wordwrap() function, shown in this example.
Asked
Active
Viewed 7,517 times
4 Answers
14
Use a java.text.BreakIterator
, something like this:
String s = ...;
int number_chars = ...;
BreakIterator bi = BreakIterator.getWordInstance();
bi.setText(s);
int first_after = bi.following(number_chars);
// to truncate:
s = s.substring(0, first_after);

David Z
- 128,184
- 27
- 255
- 279
-
This is great thanks, although would aa bi.truncateAt() have been too much to ask for? :) – Xenph Yan Feb 05 '09 at 06:02
-
1Make sure you test that number_chars is not larger than s.length(), otherwise you get an exception. FYI, I tried to edit the java to reflect this fact, but the edit was rejected. – mooreds Nov 07 '11 at 05:43
4
You can use regular expression
Matcher m = Pattern.compile("^.{0,10}\\b").matches(str);
m.find();
String first10char = m.group(0);

Dennis C
- 24,511
- 12
- 71
- 99
3
With the first approach you will end up with a length bigger than number_chars. If you need an exact maximum or less, like for a Twitter message, see my implementation below.
Note that the regexp approach uses a space to delimit the words, while BreakIterator breaks up words even if they have commas and other characters. This is more desirable.
Here is my full function:
/**
* Truncate text to the nearest word, up to a maximum length specified.
*
* @param text
* @param maxLength
* @return
*/
private String truncateText(String text, int maxLength) {
if(text != null && text.length() > maxLength) {
BreakIterator bi = BreakIterator.getWordInstance();
bi.setText(text);
if(bi.isBoundary(maxLength-1)) {
return text.substring(0, maxLength-2);
} else {
int preceding = bi.preceding(maxLength-1);
return text.substring(0, preceding-1);
}
} else {
return text;
}
}

otterslide
- 550
- 6
- 14
0
Solution with BreakIterator
is not really straightforward when breaking sentence is URL, it breaks URL not very nice way. I rather used mine solution:
public static String truncateText(String text, int maxLength) {
if (text != null && text.length() < maxLength) {
return text;
}
List<String> words = Splitter.on(" ").splitToList(text);
List<String> truncated = new ArrayList<>();
int totalCount = 0;
for (String word : words) {
int wordLength = word.length();
if (totalCount + 1 + wordLength > maxLength) { // +1 because of space
break;
}
totalCount += 1; // space
totalCount += wordLength;
truncated.add(word);
}
String truncResult = Joiner.on(" ").join(truncated);
return truncResult + " ...";
}
Splitter/Joiner is from guava. I am also adding ...
at the end in my use cas (can be ommited).

To Kra
- 3,344
- 3
- 38
- 45