Navigate back to the homepage

how to remove space from string in typescript?

Infinitbility,ย 
TypeScript,ย 
July 18th, 2022 ยท 1 min read
how to remove space from string in typescript?

Remove all space from the string in typescript

To remove all white space from the string in typescript, use the replace() method with the /\s/g regex it will remove all whitespace from the string.

1const str: string = ' first second third ';
2
3// โœ… Remove all Whitespace
4const withoutAnyWhitespace: string = str.replace(/\s/g, '');
5console.log(withoutAnyWhitespace); // ๐Ÿ‘‰๏ธ "firstsecondthird"

Remove whitespace from the beginning and end of the string in typescript

To remove space from the start and end of the string in typescript, use the trim() method it will remove space from both sides and return a new string.

1const str: string = ' first second third ';
2
3// โœ… Remove whitespace from beginning and end
4const withoutAnyWhitespace: string = str.trim();
5console.log(withoutAnyWhitespace); // ๐Ÿ‘‰๏ธ "first second third"

Remove whitespace from the beginning of the string in typescript

To remove space from the left side of the string in typescript, use the trimLeft() method it will delete space from the only left side of the string and return a new string.

1const str: string = ' first second third ';
2
3// โœ… Remove whitespace from beginning of string
4const withoutAnyWhitespace: string = str.trimLeft();
5console.log(withoutAnyWhitespace); // ๐Ÿ‘‰๏ธ "first second third "

Remove whitespace from the end of the string in typescript

To remove space from the right side of the string in typescript, use the trimRight() method it will delete space from the only right side of the string and return a new string.

1const str: string = ' first second third ';
2
3// โœ… Remove whitespace from end of string
4const withoutAnyWhitespace: string = str.trimRight();
5console.log(withoutAnyWhitespace); // ๐Ÿ‘‰๏ธ " first second third"

I hope it helps you, All the best ๐Ÿ‘.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to add double quotes in string in typescript?

how to add double quotes in string in typescript?

TypeScript, add double quotes in string example with step-by-step guide.

July 17th, 2022 ยท 1 min read
node-red dropdown example

node-red dropdown example

Example of dropdown in node-red with step by step guide.

July 17th, 2022 ยท 1 min read
ยฉ 2020โ€“2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]