What is StringUtils.truncate in Java?

Overview

truncate() is a staticthe methods in Java that can be called without creating an object of the class method of the StringUtils class which is used to truncate a given string. The method accepts:

  • a string
  • a maximum width

If the length of the string is less than the specified maximum width, then the method returns the input string. Otherwise, the method returns the substring i.e substring(str, 0, maxWidth).

If the maximum width is less than zero, then an IllegalArgumentException is thrown.

How to import StringUtils

The definition of StringUtils can be found in the Apache Commons Lang package, which we can add 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 truncate(final String str, final int maxWidth)

Parameters

  • final String str: The string to truncate.
  • final int maxWidth: The maximum length of the result string.

Return value

This method returns the truncated string.

Code

import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String s = "hellO-EDUcative";
int maxWidth = 5;
System.out.printf("The output of StringUtils.truncate() for the string - '%s' is %s", s, StringUtils.truncate(s, maxWidth));
System.out.println();
maxWidth = 20;
System.out.printf("The output of StringUtils.truncate() for the string - '%s' is %s", s, StringUtils.truncate(s, maxWidth));
System.out.println();
s = "";
System.out.printf("The output of StringUtils.truncate() for the string - '%s' is %s", s, StringUtils.truncate(s, maxWidth));
System.out.println();
s = null;
System.out.printf("The output of StringUtils.truncate() for the string - '%s' is %s", s, StringUtils.truncate(s, maxWidth));
System.out.println();
maxWidth = -1;
System.out.printf("The output of StringUtils.truncate() for the string - '%s' is %s", s, StringUtils.truncate(s, maxWidth));
System.out.println();
}
}

Example 1

  • string = "hellO-EDUcative"
  • maxWidth = 5

The method returns hellO, i.e., the substring from index zero to four.

Example 2

  • string = "hellO-EDUcative"
  • maxWidth = 20

The method returns hellO-EDUcative, i.e., the whole input string, because the maximum width is greater than the length of the string.

Example 3

  • string = ""
  • maxWidth = 20

The method returns "" as the input string is empty.

Example 4

  • string = null
  • maxWidth = 20

The method returns null as the input string is null.

Example 5

  • string = "hellO-EDUcative"
  • maxWidth = -1

The method throws an IllegalArgumentException as the specified width is less than zero.

Output

The output of the code will be as follows:


The output of StringUtils.truncate() for the string - 'hellO-EDUcative' is hellO
The output of StringUtils.truncate() for the string - 'hellO-EDUcative' is hellO-EDUcative
The output of StringUtils.truncate() for the string - '' is 
The output of StringUtils.truncate() for the string - 'null' is null
Exception in thread "main" java.lang.IllegalArgumentException: maxWith cannot be negative
	at org.apache.commons.lang3.StringUtils.truncate(StringUtils.java:9263)
	at org.apache.commons.lang3.StringUtils.truncate(StringUtils.java:9195)
	at Main.main(Main.java:24)
Attributions:
  1. undefined by undefined