Home > Web Front-end > JS Tutorial > Detailed explanation of how JavaScript implements the method of replacing characters at index in a string based on extended String

Detailed explanation of how JavaScript implements the method of replacing characters at index in a string based on extended String

黄舟
Release: 2017-06-18 13:10:28
Original
1511 people have browsed it

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);
}
Copy after login

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 = &#39;welcome to www.jb51.net&#39;;
console.log(str.replaceCharAt(8,&#39;T&#39;));
</script>
</body>
</html>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template