How to abbreviate a String in Java with Apache Commons StringUtils class

A little example on how to abbreviate a String using StringUtils class from Apache Commons Lang. The StringUtils class has two abbreviate methods.

The first takes two parameters, the string to be abbreviated and the max length of the string.

The second adds another parameter, an offset, to the parameter list.

When the string is abbreviated three dots are added to the end or beginning where the string actually is shortened. The code below shows how it’s done:

In the code above the first string is abbreviated at the end while the second is abbreviated both from the beginning and end using an offset where the abbreviation should start. The StringUtils class adds three dots where the abbreviation takes place.
Output from the code above will be:

Search for more Java info on this site here:
Custom Search

Please type any questions here.