How to remove whitespace from a string in TypeScript?

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.