Home > Web Front-end > JS Tutorial > body text

JavaScript simulates C# formatted string_javascript skills

WBOY
Release: 2016-05-16 15:42:17
Original
1034 people have browsed it

JS simulates C# string formatting operation

/***
** 功能: 字符串格式化替换操作
***/
String.prototype.format = function () {
 var args = arguments;
 return this.replace(/\{(\d+)\}/g,
 function (m, i) {
  return args[i];
 });
}
Copy after login

js implements the string processing function format() similar to c#:

Those who are familiar with C# should know that there is such a method as format(). Let’s imitate it and implement this function in JavaScript.

The code example is as follows:

String.prototype.format=function(args){ 
 if(arguments.length>0){ 
 var result=this; 
 if(arguments.length==1&&typeof(args)=="object"){ 
  for(var key in args){ 
  var reg=new RegExp("({"+key+"})","g"); 
  result=result.replace(reg, args[key]); 
  } 
 } 
 else{ 
  for(var i=0;i<arguments.length;i++){ 
  if(arguments[i]==undefined){ 
   return ""; 
  } 
  else{ 
   var reg=new RegExp ("({["+i+"]})","g"); 
   result = result.replace(reg, arguments[i]); 
  } 
  } 
 } 
 return result; 
 } 
 else{ 
 return this; 
 } 
}
var fistStr="{0}欢迎您,希望大家能够得到想要的{1}";
var secondStr="{webName}欢迎您,希望大家能够得到想要的{favoriate}";
var firstOut=fistStr.format("","东西");
var secondOut=secondStr.format({webName:"",favoriate:"东西"});
console.log(firstOut);
console.log(secondOut); 
Copy after login

The above code achieves the effect we want. Here is an introduction to its implementation process:

1. Implementation principle:

The principle is relatively simple. Let’s make a long story short. For details, please refer to the code comments. Use regular expressions to find the strings to be replaced, and then replace these strings with the specified content. In the code, some of the specified content are string literals, and some are object attribute values.

2. Code comments:

1.String.prototype.format=function(args){{}), add the instance method format to the String object through the prototype object. This method is used to process strings.

2.if(arguments.length>0), if the number of parameters passed is greater than 0.

3.var result=this, assign the reference of this to the variable result.

4.if(arguments.length==1&&typeof(args)=="object"), used to determine whether the passed parameter is an object literal.

5.for(var key in args), traverse the attributes in the object literal.

6.var reg=new RegExp("({" key "})","g"), used to match the specified string.

7.result=result.replace(reg,args[key]), replace the matching string with the attribute value.

8.else{}, if what is passed is not an object literal.

9.for(var i=0;i

10.if(arguments==undefined), if it is undefined, return an empty string.

11.var reg=new RegExp("({[" i "]})","g"), used to match the specified string.

12.result=result.replace(reg,arguments), replace.

13.return result, return the replaced string.

14.return this, if no parameters are passed, the string itself is returned.

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