Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate String on closest word boundary

Tags:

java

string

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.

like image 759
Xenph Yan Avatar asked Feb 05 '09 05:02

Xenph Yan


3 Answers

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);
like image 190
David Z Avatar answered Nov 08 '22 08:11

David Z


You can use regular expression

Matcher m = Pattern.compile("^.{0,10}\\b").matches(str);
m.find();
String first10char = m.group(0);
like image 4
Dennis C Avatar answered Nov 08 '22 07:11

Dennis C


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;
        }
    }
like image 3
otterslide Avatar answered Nov 08 '22 08:11

otterslide