String.Format function of JS imitating C #

Source: Internet
Author: User

String.prototype.format2 = function (args) {var s = this, Vals = [], rst = [], pattern =/({|})    /g, Reg = S.match (pattern), Matchs = S.match (/({+[-\w]+}+)/g); if (matchs! = null) {if (Arguments.length > 1) {for (var i = 0, c = arguments.length; i < C; i++                {if (arguments[i]! = undefined) {Vals.push (arguments[i]);        }}}} else if (Object.prototype.toString.call (args) = = = ' [Object Array] ') {vals = args;        } else if (args! = undefined && args! = null) {Vals.push (args);        } var len = vals.length; for (var i = 0, c = matchs.length; i < C; i++) {var _t = matchs[i], _tv = _t.replace (Pattern, '), _r = _t.            Match (pattern); if (_r.length% 2! = 0 | |!/^[0-9]+$/.test (_TV)) {throw new Error (' Input string is not formatted correctly.            ‘);            } var idx = parseint (_TV, 10);                if (idx >= len) {The throw new Error (' zero-based ' index must be greater than or equal to zero and is less than the size of the parameter list.            ‘);            } var _p = S.indexof (_t), _c = _t.match (/{/g). Length, _v = Vals[idx]; if (_v = = null) {throw new Error (' value cannot be null.            ‘);            } var _t1 = _t.replace (/{{/g, ' {'). Replace (/}}/g, '} '); Rst.push (s.substr (0, _p) + (_c > 1?)            (_c% 2! = 0? _t1.replace (' {' + idx + '} ', _v): _T1): _v));        s = s.substr (_p + _t.length);        } rst.push (s);    Return Rst.join ("); } else if (reg! = null && reg.length > 0) {throw new Error (' the input string is not formatted correctly.    ‘); } return s;};

Only basic functionality is implemented

Examples are as follows:

var str = ' (X{{{{0}}} {1}) {2} & X{0} {3} {4}_{5} * (X{0} + {6}) ';

Console.log (STR.FORMAT2 ([7, ' >= ', 123, ' < ', 123, ' K ', ' B ']));

var str = ' Hello: {0}, this is a pseudo C # {1} function written with {0} ';

Console.log (STR.FORMAT2 ([' JS ', ' format ']);
Console.log (STR.FORMAT2 (' JS ', ' format '));

Operation Result:

String.Format function of JS imitating C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.