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

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

黄舟
黄舟Original
2017-06-18 13:10:281489browse

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

Statement:
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