How to remove whitespace from a text string in Node.JS

Today I am going to show you how to use Node.JS to strip out whitespace from a text string — that is, any spaces, new lines, etc. To take all of the pain out of this, we will be applying a Cloudmersive API. Let’s get to it.

Our first task is that of installation of our client, which we will achieve with this command for npm install.

npm install cloudmersive-convert-api-client --save

Following installation, we are now able to call functions from our API. We will be using editTextRemoveAllWhitespace:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveConvertApiClient.EditTextApi();var request = new CloudmersiveConvertApiClient.RemoveWhitespaceFromTextRequest(); // RemoveWhitespaceFromTextRequest | Input requestvar callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.editTextRemoveAllWhitespace(request, callback);

And that wraps things up nicely. That’s really all there is to it.

Image for post
Image for post

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