How to Remove Spaces from a String in TypeScript ?
Last Updated :
07 May, 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
Using trim() method
The trim() method removes whitespace from both ends of a string. To remove spaces from within the string, we can combine trim() with replace().
Syntax:
let outputString: string = inputString.trim().replace(/\s+/g, "");
Example: In this example we removes leading and trailing spaces with trim() and then removes all spaces with replace() using a regular expression / /g and replaces them with an empty string.
JavaScript
let inputString: string = "Geeks for Geeks";
let outputString: string = inputString.trim().replace(/\s+/g, "");
console.log(outputString); // Output: GeeksforGeeks
Output:
GeeksforGeeks
Using for loop
In this approach we use a for loop to iterate over the characters of the input string. For each character we check if it is not equal to a space character ‘ ‘. If the character is not a space we will append it to the result string.
Example: In this example we iterate through each character and check it it is space. if so we dont take it in result.
JavaScript
function removeSpaces(input: string): string {
let result = '';
for (let i = 0; i < input.length; i++) {
if (input[i] !== ' ') {
result += input[i];
}
}
return result;
}
const inputString = 'Geeks For Geeks';
const stringWithoutSpaces = removeSpaces(inputString);
console.log(stringWithoutSpaces);
Output:
GeeksForGeeks
Share your thoughts in the comments
Please Login to comment...