How to trim leading and trailing Whitespace from a Text String in C# .NET Framework

Cloudmersive
1 min readJul 20, 2020

--

Today we are going to make whitespace trimming incredibly simple, the way it should be. I think we all know that in practice, it can actually be quite challenging to set up a perfectly accurate means of getting this done in C#. With that in mind, allow me to show you the easy way. Let’s get straight to the process.

First we download our package using the NuGet console:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.2.8

Now we are going to go ahead and call this function here:

using System;using System.Diagnostics;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;namespace Example{public class EditTextTrimWhitespaceExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new EditTextApi();var request = new RemoveWhitespaceFromTextRequest(); // RemoveWhitespaceFromTextRequest | Input requesttry{// Trim leading and trailing whitespace from text stringRemoveWhitespaceFromTextResponse result = apiInstance.EditTextTrimWhitespace(request);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling EditTextApi.EditTextTrimWhitespace: " + e.Message );}}}}

And would you believe me if I said that we are already finished? Yup, it’s really that easy.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet