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 anull
reference.
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;
public static String stripAccents(final String input)
input
: the input string to be stripped.The stripAccents()
method returns a new string with the diacritics removed.
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();}}
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'
"eduČativé"
The method returns eduCative
, with the accented characters replaced by their unaccented equivalent, without altering the case of the characters.
null
The method returns null
, as the string points to a null
reference.