How to Trim Leading & Trailing Whitespace from Text using Java
Unwanted whitespaces show up in text all the time, often at the beginning and end of a particular subset of text lines. These leading & trailing spaces are often superfluous to our needs. Thankfully, however, they can be removed quickly & easily with a simple programmatic service. Our Leading/Trailing Whitespace Removal API is a simple & free-to-use solution which you can easily incorporate into your application using ready-to-run Java code examples provided below in this article. This solution will trim all leading and trailing whitespace from your text string, including newlines, spaces, and other whitespace characters.
Before you get started following the below steps to incorporate this API, you’ll just need to register a free account on our website & get your secure API key ready to authenticate the service. Free-tier accounts provide a limit of 800 API calls per month (with no additional commitments), so they’re perfect for getting smaller-scale projects off the ground.
We can kick off our API client installation with Maven by first adding a reference to the repository in pom.xml:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next, we can add another reference to the dependency in pom.xml:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Moving on to our controller, let’s include our 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;
Finally, before we include the remaining code to call the function, let’s first establish our JSON or XML input request formats & get our text string argument ready:
JSON{
"TextContainingWhitespace": "string"
}XML<?xml version="1.0" encoding="UTF-8"?>
<RemoveWhitespaceFromTextRequest>
<TextContainingWhitespace>string</TextContainingWhitespace>
</RemoveWhitespaceFromTextRequest>
Now let’s authenticate our API key where instructed by the comments & call the 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.editTextTrimWhitespace(request);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EditTextApi#editTextTrimWhitespace");
e.printStackTrace();
}