...

/

Remove Spaces and Tabs in a String

Remove Spaces and Tabs in a String

In this lesson, we'll learn how to remove tabs from a string using recursion.

What does “Removing Tabs and Spaces from a String” mean?

Our task is to remove all tabs and spaces from a string. This includes the character \t and " " from a string.

Removing spaces from a string.
Removing spaces from a string.

Implementation

Press + to interact
function remove(string) {
// Base case
if (string.length == 0) {
return "";
}
// Recursive case
if (string[0] == "\t" || string[0] == " ") {
return remove(string.substr(1));
}
else {
return string[0] + remove(string.substr(1));
}
}
// Driver Code
var myString = "Hello\tWorld";
console.log(remove(myString));

Explanation

Removing tabs from a null string "" ...