What is the StringUtils.replace() method in Java?
Overview
In Java, replace() is a static method of the StringUtils class which is used to replace the search string with a replacement string. The method optionally accepts the first maximum number of matches of the search string to be replaced in the given text. The string matching here is case-sensitive in nature.
Note: Refer What is StringUtils.replaceIgnoreCase in Java? for case-insensitive matching and replacement.
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>
Note: For other versions of the commons-lang package, refer to the Maven Repository.
We can import the StringUtils class as follows:
import org.apache.commons.lang3.StringUtils;
Syntax
public static String replace(final String text, final String searchString, final String replacement, final int max)
Parameters
final String text: Text to search and replace in.final String searchString: The string to search for.final String replacement: The replacement string.final int max: The first maximum number of values to replace.
Return value
This method returns the text with the search string replaced with the replacement string.
Overloaded methods:
public static String replace(final String text, final String searchString, final String replacement)
Code
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>Explanation
The maven dependency for StringUtils is included in the pom.xml file:
Main.java
- Line 1: We import the
StringUtils. - Line 6: We define a string called text.
- Line 7: We define the search string called
searchString. - Line 8 - We define the replacement string called
replacementString. - Line 9: We define the maximum number of replacements called
maxReplacements. - Lines 10-12 - We invoke the
replace()method by passingtext,searchString,replacementString, andmaxReplacementsas parameters. The output is printed on to console.
There are two observations to be made in the output:
- The comparison and replacement are case-sensitive in nature.
- Only the first
maxReplacementsoccurrences of thesearchStringare replaced.