
- 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
Function that only replaces character from string after specified appearances in JavaScript
We are required to write a JavaScript function that takes in a string as the first argument, a number, say n, as the second argument and a character, say c, as the third argument. The function should replace the nth appearance of any character with the character provided as the third argument and return the new string.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const str = 'This is a sample string'; const num = 2; const char = '*'; const replaceNthAppearance = (str, num, char) => { const creds = str.split('').reduce((acc, val, ind, arr) => { let { res, map } = acc; if(!map.has(val)){ map.set(val, 1); if(num === 0){ res += char; }else{ res += val; } }else{ const freq = map.get(val); if(num - freq === 1){ res += char; }else{ res += val; }; map.set(val, freq+1); }; return { res, map }; }, { res: '', map: new Map() }); return creds.res; } console.log(replaceNthAppearance(str, num, char));
Output
The output in the console will be −
This ***a s*mple string
- Related Articles
- How to get a part of string after a specified character in JavaScript?
- Count appearances of a string in another - JavaScript
- Print the string after the specified character has occurred given no. of times in C Program
- String function to replace nth occurrence of a character in a string JavaScript
- Reversing consonants only from a string in JavaScript
- How to remove only last character from a string vector in R?
- Left pad a String with a specified character in Java
- Splitting the string after the specified separator in Golang
- How MySQL REPLACE() function replaces strings in multiple records?
- Rearrange string so that same character become n distance apart JavaScript
- How to cut only the first character in MySQL string?
- Insert a character after every n characters in JavaScript
- Convert the string into palindrome string by changing only one character in C++
- Generating random string of specified length in JavaScript
- Return the index of first character that appears twice in a string in JavaScript

Advertisements