How to Remove Whitespace from a Text String using Java
Whitespace can work for us or against us within a text file, depending on what our uses are for the text contents within it. When it comes to programming & data entry in particular, unwanted whitespace can be a real hinderance to productivity, adding extra steps to each process & even causing significant formatting issues at times. Thankfully, our Whitespace Removal API makes it straightforward to programmatically clean up the whitespace in a text string; all you need to do is structure your API call using the code examples provided below, and authenticate the service with a free-tier Cloudmersive API key. You can get your API key by registering a free account on our website; with that account, you’ll get a limit of 800 API calls per month with zero additional commitments — perfect for getting a small-scale project off the ground without worrying about breaking the bank.
Our first step is to install the API client with Maven. Let’s add a reference to 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 our installation step; now we can turn our attention to the controller & include the following imports at the top of our file:
// 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;
Now we can structure our input request like so:
{
"TextContainingWhitespace": "string"
}
And pass our argument through the text conversion function:
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();
RemoveWhitespaceFromTextRequest request = new RemoveWhitespaceFromTextRequest(); // RemoveWhitespaceFromTextRequest | Input request
try {
RemoveWhitespaceFromTextResponse result = apiInstance.editTextRemoveAllWhitespace(request);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditTextApi#editTextRemoveAllWhitespace");
e.printStackTrace();
}