Categories
TypeScript Answers

How to remove whitespace from a string in TypeScript?

Spread the love

Sometimes, we want to remove whitespace from a string in TypeScript.

In this article, we’ll look at how to remove whitespace from a string in TypeScript.

How to remove whitespace from a string in TypeScript?

To remove whitespace from a string in TypeScript, we can use the string replace method.

For instance, we write

const out = "hello world".replace(/\s/g, "");
console.log(out);

to call replace on 'hello world' with a regex to match all whitespaces and replace them all with empty strings.

Conclusion

To remove whitespace from a string in TypeScript, we can use the string replace method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *