How to Remove Spaces from a String in TypeScript ?
Last Updated :
12 Feb, 2024
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:
Using split() and join() methods
The split() method mainly divides the string into the array of substrings and join(‘ ‘) then concatenates the substrings by removing the spaces from the string.
Syntax:
let outputString: string = inputString.split(' ').join('');
Example: The below example removes spaces from the inputString by splitting it into an array at each space using split(‘ ‘) and then joining the array elements into a single string without spaces using join(”), resulting in the output “GeeksforGeeks”.
Javascript
let inputString: string = "Geeks for Geeks" ;
let outputString: string = inputString.split( " " ).join( "" );
console.log(outputString);
|
Output:
GeeksforGeeks
Using replace() method
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.
Javascript
let inputString: string = "Geeks for Geeks" ;
let outputString: string = inputString.replace(/ /g, "" );
console.log(outputString);
|
Output:
GeeksforGeeks
Using RegExp
The regular expression with the replace() method replaces one ore more whitespace characters with an empty string by removing the spaces from the original string.
Syntax:
let outputString: string = inputString.split(/\s+/).join('');
Example: The below example removes spaces from the inputString using regular expression (/\s+/), splitting it into an array at one or more whitespace characters and then joining the array elements into a single string without spaces using join(”).
Javascript
let inputString: string = "Geeks for Geeks" ;
let outputString: string = inputString.split(/\s+/).join( "" );
console.log(outputString);
|
Output:
GeeksforGeeks
Using filter() method
The filter() method along with split() and join(‘ ‘) method constructs the new array of characters by exclusing the spaces. Using the join(‘ ‘) method the charcaters are contatenated back into the single string.
Syntax:
let outputString: string = inputString.split('').filter(char => char !== ' ').join('');
Example: The below example removes spaces from the inputString using the filter() method along with split(”) and join(”). It splits the string into an array of characters, filters out the space characters, and then joins the remaining characters back into a single string, resulting in the output “GeeksforGeeks”.
Javascript
let inputString: string = "Geeks for Geeks" ;
let outputString: string = inputString
.split( "" )
.filter((char) => char !== " " )
.join( "" );
console.log(outputString);
|
Output:
GeeksforGeeks
Share your thoughts in the comments
Please Login to comment...