When you remove all whitespace from a string in Typescript, it will help you to configure user input. So how to do it? Let’s go into detail.
Remove all whitespace from a string in TypeScript
Use trim() method
The trim() method will help you remove whitespace from both sides of your string, and the trim() method will return a new trim string.
Syntax:
trim()
Example:
const aString = "Hello From Learn Share IT "; const trimedSring = aString.trim(); console.log(trimedSring);
Output:
"Hello From Learn Share IT"
But the trim() method can only remove the whitespace from both sides of your string which remove white space from the start from the first whitespace to the first letter and from the last letter to the last whitespace. If you want to trim only on one side, it also has the trimLeft() method and trimEnd() method to help you trim only on one side.
Example:
const aString = " Learn ShareIT "; const trimedLeftSring = aString.trimLeft(); const trimedRightString = aString.trimEnd(); console.log(trimedLeftSring); console.log(trimedRightString);
Output:
"Learn ShareIT "
"
Learn ShareIT"
So if you want to remove all whitespace that includes inside the string, you will have to use another way.
Use replaceAll method
The replaceAll method will help you replace all specified strings in a string.
Syntax:
string.replaceAll(pattern,replacement)
You can watch more about it here
Example:
const aString = " Learn ShareIT "; const replacedString = aString.replaceAll(" ", ""); console.log(replacedString);
Output:
“LearnShareIT”
Here I replace all space with empty so the origin string will turn into a string without space.
Use replace method
The replace() will search and replace the first matching pattern and return it.
string.replace(pattern,replacement)
You can read more about it here
Example:
const aString = "Learn Share IT"; const replacedString = aString.replace(" ", ""); console.log(replacedString);
Output:
"LearnShare IT"
So if you want to replace all strings like replaceAll method, you will have to RegExp reference. You can read more about RegExp here. But in our situation, RegExp provides a modification that can help you apply the effect to all patterns that match the condition, that is /g/stands for global.
Example:
const aString = "Learn Share IT"; const replacedString = aString.replace(/ /g, ""); console.log(replacedString);
Output:
"LearnShareIT"
All the strings inside the double slash “//” will be searched globally.
Summary
In this tutorial, we showed and explaind to you how to remove all whitespace from a string in Typescript. You can use trim() or replace() or replaceAll() to remove all whitespace. If you have any questions or problems, please comment below. Thanks for reading!
Maybe you are interested:
- Convert a String to a Boolean in TypeScript
- How to parse a JSON string in TypeScript
- How To Check If Two Strings Are Equal In Typescript

Hello, guys! I hope that my knowledge in HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, Python, MySQL, and npm computer languages may be of use to you. I’m Brent Johnson, a software developer.
Name of the university: HOU
Major: IT
Programming Languages: HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, PyThon, MySQL, npm