What is StringUtils.abbreviate in Java?

Overview

abbreviate is a static method of the StringUtils class that is used to abbreviate the given string with the given replacement character.

An abbreviation is a shortened form of a word or phrase.

How to import StringUtils

StringUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml file.


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

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the StringUtils class as follows:


import org.apache.commons.lang3.StringUtils;

Syntax


public static String abbreviate(final String str, final String abbrevMarker, int offset, final int maxWidth)

Parameters

The function takes the following parameters:

  • final String str: the string to check.
  • final String abbrevMarker: the string to use as the replacement marker.
  • int offset: the left edge of the source string.
  • final int maxWidth: the maximum length of the abbreviated string; this parameter must take a minimum value of 4.

Note: The left edge will not always be the first character after the replacement marker or the leftmost character in the result, but it will occur somewhere in the result.

Return value

This method returns the abbreviated string replaced with the given marker.

Overloaded methods

public static String abbreviate(final String str, final int maxWidth)

public static String abbreviate(final String str, final int offset, final int maxWidth)

public static String abbreviate(final String str, final String abbrevMarker, final int maxWidth)

Code

import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String s = "educative";
int offset = 3;
int maxWidth = 5;
String abbrMarker = "...";
System.out.printf("Abbreviation of %s is %s", s, StringUtils.abbreviate(s, abbrMarker, offset, maxWidth));
System.out.println();
s = "educative";
offset = 3;
maxWidth = 6;
abbrMarker = "##";
System.out.printf("Abbreviation of %s is %s", s, StringUtils.abbreviate(s, abbrMarker, offset, maxWidth));
System.out.println();
s = "educative.io";
offset = 4;
maxWidth = 10;
abbrMarker = "##";
System.out.printf("Abbreviation of %s is %s", s, StringUtils.abbreviate(s, abbrMarker, offset, maxWidth));
}
}

Explanation

  • string = "educative"
  • offset = 3
  • maximum width = 5
  • abbreviation marker = "..."

The method returns ed..., as the offset is 3, the maximum width of the abbreviated string is 5, and the abbreviation marker is ....

  • string = "educative"
  • offset = 3
  • maximum width = 6
  • abbreviation marker = "##"

The method returns educ##, as the offset is 3, the maximum width of the abbreviated string is 6, and the abbreviation marker is ##.

  • string = "educative.io"
  • offset = 4
  • maximum width = 10
  • abbreviation marker = "##"

The method returns ##ative.io, as the offset is 4, the maximum width of the abbreviated string is 10, and the abbreviation marker is ##.

Output

The output of the code will be as follows:


Abbreviation of educative is ed...
Abbreviation of educative is educ##
Abbreviation of educative.io is ##ative.io