How to Replace a String in Text with Another String Value using Java

Cloudmersive
2 min readSep 9, 2022

--

When we can’t use built-in text editor tools to find & replace values in a text string, we must instead rely on other programmatic means to do so; otherwise, we’ll waste time trying to track down and identify each string individually.

Thankfully, there’s an API for that. The Cloudmersive Text String Replacement API can be included in your project as a simple & easy-to-use service, requiring basic request parameters to target and replace a given string. With a free-tier Cloudmersive Account, you can take advantage of this API for free (free-tier accounts allow up to 800 API calls per month: perfect for getting small-scale projects of the ground in a cost-effective way). In the below walk through, I’ll demonstrate how to install the API client & structure your call/request in a few simple steps.

To begin API client installation with Maven, let’s include a reference in the pom.xml repository:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next, let’s add one to the pom.xml dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

That concludes installation; now we can shift focus to our controller & add in the following imports:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.EditTextApi;EditTextApi apiInstance

Before we get around to calling the function, let’s take a moment to structure our input request in either of the following formats:

JSON{
"TextContent": "string",
"TargetString": "string",
"ReplaceWithString": "string"
}
XML<?xml version="1.0" encoding="UTF-8"?>
<ReplaceStringSimpleRequest>
<TextContent>string</TextContent>
<TargetString>string</TargetString>
<ReplaceWithString>string</ReplaceWithString>
</ReplaceStringSimpleRequest>

Lastly, let’s pass our argument through the function. Don’t forget to include your API key where shown in the comments:

ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
EditTextApi apiInstance = new EditTextApi();
ReplaceStringSimpleRequest request = new ReplaceStringSimpleRequest(); // ReplaceStringSimpleRequest | Input request
try {
ReplaceStringSimpleResponse result = apiInstance.editTextReplaceSimple(request);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditTextApi#editTextReplaceSimple");
e.printStackTrace();
}

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet