How to trim leading and trailing Whitespace from a Text String in Node.JS

The goal of our tutorial today is to show you how to remove whitespace from the beginning and end of a given text string. Before you dust off your coding hat, what if I told you that we weren’t going to need much coding at all? With the help of a Cloudmersive API, we can get this done very easily indeed. Let’s look at how.

Installing our client is the first step. Simply activate this command for npm install.

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

Now we can use editTextTrimWhitespace and provide it with our text string to trim.

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.editTextTrimWhitespace(request, callback);

And we are already done!

Image for post
Image for post

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