- By replace() method
- By split() and join() methods
- By slice() and concatenation methods
Let’s explore each…
#1 – By replace() method
How it works
The replace() method searches a string for a specified value and returns a new string where the specified value is replaced by another value. It only replaces the first match it finds.
- The “string” variable is initially set to “Hello, World!”
- The replace() method is called on the “string” variable.
- The method returns a new string where the first match of the specified value is replaced, which is then stored in the “newString” variable.
#2 – By split() and join() methods
This method involves using the split() function to separate the original string into an array of substrings based on the string you want to remove, and then using join() function to reassemble that array into a new string without the specified string. For instance, if you want to remove the string ‘world’ from ‘Hello world’, you would split the original string by ‘world’ and then join it back together.
var str = "Hello world"; var removeStr = "world"; var newStr = str.split(removeStr).join(''); console.log(newStr);
How it works
The split() function is used to divide a string into an array of substrings, and it returns the new array. The join() function is used to join the elements of an array into a string, and it returns the string. When using these two methods together, you can effectively remove a specific substring from your original string.
- The string that you want to remove from the original string is passed into the split() function as a parameter. This divides the original string into an array of substrings wherever it finds the specified string.
- The split function results in an array with substrings [“Hello “, “”] in our given example.
- Then, the array resulting from the split() function is immediately passed into the join() function which concatenates all the substrings in the array into a new string, and the specified string which you want to remove will not be included in the new string because it was used as the separator in the split() function.
- The join function results in a new string “Hello “.
- In this way, the specified string is removed from the original string.
#3 – By slice() and concatenation methods
The operation involves finding the start and end indices of the substring we want to remove using the
indexOf() method, then using the
slice() method to cut the original string at these indices, and finally concatenating the two sliced parts to form the resultant string without the unwanted substring. Let’s understand this better with an example.
How it works
slice() and string concatenation techniques. The steps are as follows:
- First, we define the original string and the string we want to remove.
- Next, we find the starting index of the substring we want to remove with the
- Then, we calculate the end index of the substring by adding the length of the substring to its start index.
- Finally, we use the
slice()method twice to cut the original string into two parts at the start and end indices of the substring, and concatenate these two parts to get the final string without the unwanted substring.