How to use replace() method In Typescript
The replace() method with the (‘ / /g) replaces all the occurnces of spaces within an empty string, which elimiates the spaces from the original string.
Syntax:
let outputString: string = inputString.replace(/ /g, '');
Example: The below example removes spaces from the inputString using the replace(/ /g, ”) method.
let inputString: string = "Geeks for Geeks";
let outputString: string = inputString.replace(/ /g, "");
console.log(outputString);
Output:
w3wiki
How to Remove Spaces from a String in TypeScript ?
Typescript allows us to remove the spaces between the characters or the entire words of the string. The combination of various inbuilt functions can be implemented to remove the spaces from the string. In Typescript, removing spaces from a string can be done using various approaches which are as follows:
Table of Content
- Using split() and join() methods
- Using replace() method
- Using RegExp
- Using filter() method
- Using trim() method
- Using for loop