Reputation: 83981
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.
Upvotes: 10
Views: 7745
Reputation: 3578
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).
Upvotes: 0
Reputation: 560
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;
}
}
Upvotes: 3
Reputation: 24747
You can use regular expression
Matcher m = Pattern.compile("^.{0,10}\\b").matches(str);
m.find();
String first10char = m.group(0);
Upvotes: 4
Reputation: 131550
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);
Upvotes: 14