

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a polyfill to replace nth occurrence of a string JavaScript
Let’s say, we have created a polyfill function removeStr() that takes in three arguments, namely −
subStr → the occurrence of which String is to be removed
num → it’s a Number (num)th occurrence of subStr is to be removed from string
The function should return the new if the removal of the subStr from the string was successfully, otherwise it should return -1 in all cases.
For example −
const str = 'drsfgdrrtdr'; console.log(str.removeStr('dr', 3));
Expected output −
'drsfgdrrt'
Let’s write the code for this −
Example
const str = 'drsfgdrrtdr'; const subStr = 'dr'; const num = 2; removeStr = function(subStr, num){ if(!this.includes(subStr)){ return -1; } let start = 0, end = subStr.length; let occurences = 0; for(; ;end < this.length){ if(this.substring(start, end) === subStr){ occurences++; }; if(occurences === num){ return this.substring(0, start) + this.substring(end, this.length); }; end++; start++; } return -1; } String.prototype.removeStr = removeStr; console.log(str.removeStr(subStr, num));
Output
Output for this code in the console will be −
drsfgrtdr
Understanding the code −
The removeStr() function firstly checks if there’s not even a single occurrence of subStr then we should exit out and return -1.
Then it uses the sliding window algorithm to record the number of occurrences of subStr in string(the size of window being equal to length of subStr).
Initially we start from leftmost window then we keep sliding our window till the end of window reaches the end of original string.
If in our way, the count of occurrences equals the required occurrence, we trim that occurrence from the string and return the new string thus obtained.
If we iterate through the whole string, it means that there are not enough occurrences of subStr in string and in which case we should return -1 and exit the function
Lastly, we add the removeStr property to String.prototype so that we can call it as a string function.
- Related Questions & Answers
- String function to replace nth occurrence of a character in a string JavaScript
- C program to replace all occurrence of a character in a string
- How to find the nth occurrence of substring in a string in Python?
- Replace words of a string - JavaScript
- Python - Replace duplicate Occurrence in String
- Replace first occurrence of a character in Java
- How to replace the last occurrence of an expression in a string in Python?
- How to count the occurrence of a specific string in a string in JavaScript
- How to replace all occurrences of a string in JavaScript?
- Replacing every nth instance of characters in a string - JavaScript
- Does JavaScript have a method to replace part of a string without creating a new string?
- JavaScript - Writing a string function to replace the kth appearance of a character
- Replace alphabets with nth forward alphabet in JavaScript
- Replace all occurrence of specific words in a sentence based on an array of words in JavaScript
- C# Program to find number of occurrence of a character in a String