How to replace a text string with another string value in Java

Search and replace is one of the most useful features to have in so many different applications. This crucial role cannot always be filled so easily by taking a direct, manual approach. If you would like a nice shortcut to achieve this instead, then just keep reading.

First we need a Jitpack reference in our repositories section of Maven POM.

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

Then for our dependency of our client library:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v3.54</version>
</dependency>
</dependencies>

And finally you can use this function call here to perform the search and replace:

// 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;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: ApikeyApiKeyAuth 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 requesttry {ReplaceStringSimpleResponse result = apiInstance.editTextReplaceSimple(request);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling EditTextApi#editTextReplaceSimple");e.printStackTrace();}

Done!

Image for post
Image for post

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store