![]() |
The replaceAt() method is useful for removing the character at a given place. Unfortunately, the replaceAt() method is not a built-in method in JavaScript. It would need to be defined by the user in order to use it. One possible implementation of a replaceAt() function could take a string and two index arguments, and use the JavaScript splice() method to remove the character at the specified index and insert the new character in its place. We declare this method by specifying it in String.prototype. Syntax: string.replaceAt(replace_position, replace_char); Parameters: This method accepts two parameters.
Return Value: This method returns a new string where the desired values have been replaced. Example: Input: str = hello world replace_position = 2 replace_char = k Output: hello world Input: str = Geeks for geeks replace_position = 4 replace_char = d Output: Geeks for geeks Here is an example of how this function could be defined: Example 1: Javascript
Output: Hallo World Example 2: Javascript
Output: Geeks for geeks We have a complete list of Javascript Strings methods, to check those please go through Javascript String Complete reference article. |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |