How to remove whitespace from a text string in Java

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.


Dependency reference.


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.

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