
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript - Writing a string function to replace the kth appearance of a character
Let’s say, we are required to write a String.prototype function that takes in three arguments.
- First argument is string that should be searched for substrings
- Second argument is the string, the occurrence of which String to be removed
- Third argument is a Number say n, nth occurrence of substring to be removed from string.
The function should return the new string if the removal of the subStr from the string was successful, otherwise it should return -1 in all cases.
Example
Following is the code −
const str = 'jkdsttjkdsre'; const subStr = 'jk'; 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++; } } String.prototype.removeStr = removeStr; console.log(str.removeStr(subStr, num));
This 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 there are not enough occurrences of subStr in string and in that 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.
Output
This will produce the following output in console −
jkdsttdsre
- Related Articles
- String function to replace nth occurrence of a character in a string JavaScript
- Finding the longest consecutive appearance of a character in another string using JavaScript
- Program to find the kth character after decrypting a string in C++
- Java Program to Replace the Spaces of a String with a Specific Character
- Golang program to replace the spaces of string with a specific character
- C# Program to replace a special character from a String
- C program to replace all occurrence of a character in a string
- Replace words of a string - JavaScript
- Replace Character in a String in Java without using replace() method
- Java Program to replace all occurrences of a given character in a string
- Writing a custom URL shortener function in JavaScript
- Create a polyfill to replace nth occurrence of a string JavaScript
- How to replace all occurrences of a string in JavaScript?
- How to replace a character in Objective-C String for iPhone SDK?
- C# program to replace n-th character from a given index in a string
