What is StringUtils.stripAccents in Java?

stripAccents() is a static method of the StringUtils class that is used to remove diacritics from the input string.

The stripAccents() method replaces the accented characters with their unaccented equivalent without altering the case of the character. For example, the character Č is replaced by C.

The method returns null if the input string points to a null reference.

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 stripAccents(final String input)

Parameters

  • input: the input string to be stripped.

Return value

The stripAccents() method returns a new string with the diacritics removed.

Code

import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String s = "eduČativé";
System.out.printf("The output of StringUtils.stripAccents() for the string - '%s' is '%s'", s, StringUtils.stripAccents(s));
System.out.println();
s = null;
System.out.printf("The output of StringUtils.stripAccents() for the string - '%s' is '%s'", s, StringUtils.stripAccents(s));
System.out.println();
}
}

Output

The output of the code will be as follows.

The output of StringUtils.stripAccents() for the string - 'eduČativé' is 'eduCative'
The output of StringUtils.stripAccents() for the string - 'null' is 'null'

Explanation

Example 1

  • string - "eduČativé"

The method returns eduCative, with the accented characters replaced by their unaccented equivalent, without altering the case of the characters.

Example 2

  • string - null

The method returns null, as the string points to a null reference.

Free Resources