Home > Article > Web Front-end > Detailed explanation of how JavaScript implements the method of replacing characters at index in a string based on extended String
This article mainly introduces the method of JavaScript based on the extension String to replace the character at index in string, involving the use of javascript substrRelated implementation techniques for string replacement operations, friends in need can refer to the following
The example in this article describes the JavaScript method of replacing the character at the index in the string based on extended String. Share it with everyone for your reference, as follows:
Core code:
##
String.prototype.replaceCharAt = function(n,c){ return this.substr(0, n)+ c + this.substr(n+1,this.length-1-n); }Usage example:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>JS字符替换</title> </head> <body> <script > String.prototype.replaceCharAt = function(n,c){ return this.substr(0, n)+ c + this.substr(n+1,this.length-1-n); } var str = 'welcome to www.jb51.net'; console.log(str.replaceCharAt(8,'T')); </script> </body> </html>The running effect diagram is as follows:
The above is the detailed content of Detailed explanation of how JavaScript implements the method of replacing characters at index in a string based on extended String. For more information, please follow other related articles on the PHP Chinese website!