Javascript simulation to implement the C # string. Format Function

Source: Internet
Author: User

Some time ago, it took a lot of time to parse the blog garden list page and details page HTML to generate Win8 client data sources, thanks to thinkwang for providing the link information of the open APIs in the blog garden in the comment "Win8 client development record 2-interface draft, in the past two days, I have been working on replacing the old HTML parsing code with the blog garden WCF Service. Although the Open API service of the blog garden has limited functions and the data is not very uniform, however, this greatly reduces the amount of code and parsing exceptions. For example, some people add custom styles, codes, and some blogs use custom URLs instead of testing them themselves, A lot of problems are hard to find, which leads to a lot of code judgment conditions. Without comments and instructions, it will be a bit painful to look back at the code in two days ..

The URL format of the Open API service in the blog Park is as follows: Rule} or rule. In this way, these URLs are sent to dataprovider for processing. dataprovider must internally maintain the index of the current page and the number of each page, finally, XML data is obtained through XMLHttpRequest or backgroundtransfer. Therefore, each dataprovider creation is initialized in the format of callback}. In this case, {0} needs to be replaced with pageindex for each HTTP request, replace {1} with pagesize. net C # has a very easy-to-use string. the format static function can be:

 string.Format("http://wcf.open.cnblogs.com/blog/sitehome/paged/{0}/{1}", pageIndex, pageSize);

Because the string. Format function is used in many places, a simple version is implemented using JS:

 String.format = function ()        {            var formatStr = arguments[0];            if ( typeof formatStr === 'string' )            {                var pattern,                    length = arguments.length;                for ( var i = 1; i < length; i++ )                {                    pattern = new RegExp( '\\{' + ( i - 1 ) + '\\}', 'g' );                    formatStr = formatStr.replace( pattern, arguments[i] );                }            } else            {                formatStr = '';            }            return formatStr;        };

The above Code adds a static format Method to the javascript string class, and its usage is exactly the same as the string. Format of C #. The test is as follows:

String. Format ('HTTP: // wcf.open.cnblogs.com/blog/sitehome/paged/%0%/%1%' ,) output: "http://wcf.open.cnblogs.com/blog/sitehome/paged/1/20"
String. format ('{0} + {0} + {1} = {2}', 1, 1 + 1 + 2) Output: "1 + 1 + 2 = 4"
String. Format ({Name: 'leonwang'}, 'Hello, World') Output :""

If the first parameter is not of the string type, an empty string is returned without further processing.

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.