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 method. We can download the apache-commons maven dependency from here.abbreviate()
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:
- The method checks if the input string
strisnull. If it is, the method returnsnull. - The method checks if the maximum width
maxWidthis greater than or equal to the length of the abbreviation markerabbrevMarker. If it is not, the method throws anIllegalArgumentExceptionwith an appropriate message. - The method checks if the length of the input string
stris less than or equal to the maximum widthmaxWidth. If it is, the method returns the input stringstrunchanged. - The method checks if the offset
offsetis greater than the length of the input stringstr. If it is, the method sets the offset to the length of the input stringstr. - The method checks if the remaining length of the input string
strafter the offsetoffsetis less than the maximum widthmaxWidthminus the length of the abbreviation markerabbrevMarker. If it is, the method sets the offset to the difference between the length of the input stringstrand the maximum widthmaxWidthplus the length of the abbreviation markerabbrevMarker. - The method checks if the offset
offsetis less than or equal to the length of the abbreviation markerabbrevMarker. If it is, the method returns a substring of the input stringstrfrom the beginning to the maximum widthmaxWidthminus the length of the abbreviation markerabbrevMarker, concatenated with the abbreviation markerabbrevMarker. - The method checks if the maximum width
maxWidthis less than 4. If it is, the method throws an `
That’s all about StringUtils abbreviate() example in Java.
See docs
Related post
- CollectionUtils isEmpty() Example in Java.
- StringUtils isEmpty() and IsBlank() Example in Java.
- StringUtils join() Example in Java.
- CollectionUtils union() Example in Java
- CollectionUtils intersection() Example in Java
- CollectionUtils isEqualCollection() Example in Java
- StringUtils isAlpha() example in Java
- StringUtils isNumeric() example in Java
