How to remove whitespace from a text string in Java

Cloudmersive
1 min readFeb 14, 2020

Today we are looking into an easy method for removing all whitespace characters (such as new lines and spaces) from any text string.

Our first task is the installation of our library. To get this started, your Maven POM file will need to have the following references.

Repository reference.

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

Dependency reference.

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

With our library ready, we can invoke editTextRemoveAllWhitespace.

// 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();RemoveWhitespaceFromTextRequest request = new RemoveWhitespaceFromTextRequest(); // RemoveWhitespaceFromTextRequest | Input requesttry {RemoveWhitespaceFromTextResponse result = apiInstance.editTextRemoveAllWhitespace(request);System.out.println(result);} catch (ApiException e) {System.err.println("Exception when calling EditTextApi#editTextRemoveAllWhitespace");e.printStackTrace();}

And it’s just as simple as that. You might also be interested in some of our other API functions for such useful features as text recognition for document scans, format conversion, and file editing.

--

--

Cloudmersive

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