StringUtils abbreviate() example in Java

In this post, we will see org.apache.commons.lang3.StringUtils abbreviate() example in Java.

The StringUtils class in Apache Commons Lang is a utility class that provides various methods to work with strings in Java. The abbreviate() method is one such method that can be used to abbreviate a string to a specified length.

Here’s an example of how to use the abbreviate() method in Java.

import org.apache.commons.lang3.StringUtils;

public class StringUtilsExample {
    public static void main(String[] args) {
        String longString = "This is a very long string that needs to be abbreviated.";
        
        String abbreviatedString = StringUtils.abbreviate(longString, 20);
        
        System.out.println(abbreviatedString);
    }
}

The output of the above code snippet

This is a very lon…

In this example, we import the StringUtils class from the org.apache.commons.lang3 package. We then create a longString variable that contains a long string that we want to abbreviate.

We call the abbreviate() method of the StringUtils class and pass in the longString and the maximum length we want the abbreviated string to be (in this case, 20 characters).

The abbreviate() method then returns an abbreviated version of the longString that is no longer than 20 characters. This abbreviated string is stored in the abbreviatedString variable.

Finally, we print out the abbreviatedString variable to the console, which will output.

Note that the abbreviate() method adds an ellipsis (...) to the end of the string to indicate that it has been abbreviated. If the original string is shorter than the specified length, the abbreviate() method simply returns the original string unchanged.

Additinal note – We need to add the below dependency in maven to use org.apache.commons.lang3.StringUtils abbreviate() method. We can download the apache-commons maven dependency from here.

pom.xml changes

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>

The internal implementation of StringUtils abbreviate() method

public static String abbreviate(String str, int maxWidth) {
    return abbreviate(str, "...", 0, maxWidth);
}

public static String abbreviate(String str, String abbrevMarker, int offset, int maxWidth) {
    if (str == null) {
        return null;
    }
    if (maxWidth < abbrevMarker.length()) {
        throw new IllegalArgumentException("Minimum abbreviation width is " + abbrevMarker.length());
    }
    if (str.length() <= maxWidth) {
        return str;
    }
    if (offset > str.length()) {
        offset = str.length();
    }
    if (str.length() - offset < maxWidth - abbrevMarker.length()) {
        offset = str.length() - (maxWidth - abbrevMarker.length());
    }
    if (offset <= abbrevMarker.length()) {
        return str.substring(0, maxWidth - abbrevMarker.length()) + abbrevMarker;
    }
    if (maxWidth < 4) {
        throw new IllegalArgumentException("Minimum abbreviation width with marker is 4");
    }
    if (maxWidth - abbrevMarker.length() < 1) {
        throw new IllegalArgumentException("Minimum abbreviation width with marker is 1");
    }
    if (offset + maxWidth - abbrevMarker.length() < str.length()) {
        return abbrevMarker + abbreviate(str.substring(offset), abbrevMarker, 0, maxWidth - abbrevMarker.length());
    }
    return abbrevMarker + str.substring(str.length() - (maxWidth - abbrevMarker.length()));
}

The StringUtils.abbreviate() method has two overloaded methods. The first one calls the second one with default arguments: the abbreviation marker is "..." and the offset is 0.

The second method takes four arguments: the input string str, the abbreviation marker abbrevMarker, the offset from the beginning of the string offset, and the maximum width of the abbreviated string maxWidth.

Here is a step-by-step explanation of how the StringUtils.abbreviate() method works:

  1. The method checks if the input string str is null. If it is, the method returns null.
  2. The method checks if the maximum width maxWidth is greater than or equal to the length of the abbreviation marker abbrevMarker. If it is not, the method throws an IllegalArgumentException with an appropriate message.
  3. The method checks if the length of the input string str is less than or equal to the maximum width maxWidth. If it is, the method returns the input string str unchanged.
  4. The method checks if the offset offset is greater than the length of the input string str. If it is, the method sets the offset to the length of the input string str.
  5. The method checks if the remaining length of the input string str after the offset offset is less than the maximum width maxWidth minus the length of the abbreviation marker abbrevMarker. If it is, the method sets the offset to the difference between the length of the input string str and the maximum width maxWidth plus the length of the abbreviation marker abbrevMarker.
  6. The method checks if the offset offset is less than or equal to the length of the abbreviation marker abbrevMarker. If it is, the method returns a substring of the input string str from the beginning to the maximum width maxWidth minus the length of the abbreviation marker abbrevMarker, concatenated with the abbreviation marker abbrevMarker.
  7. The method checks if the maximum width maxWidth is less than 4. If it is, the method throws an `

That’s all about StringUtils abbreviate() example in Java.

See docs

Related post