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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.