Sprintf, printf, and sprintfprintf are implemented in JavaScript.

Source: Internet
Author: User

Sprintf, printf, and sprintfprintf are implemented in JavaScript.

Use JavaScript to implement the sprintf/printf functions in most languages.

Http://www.webtoolkit.info/javascript-sprintf.html: relatively complete simulation of sprintf functions. Available formatting wildcards:
1. %-Return percentage itself
2.% B-binary number
3. characters corresponding to % c-ASCII
4.% d-integer
5.% f-floating point number
6.% o-octal Digit
7.% s-string
8.% x-16 digits (in lower case)
9.% X-16 digits (uppercase letters)

Options available between the % and wildcard characters include (for example, %. 2f ):

1. + (force the + and-signs to be displayed before the number to mark them as positive and negative numbers. By default, only negative numbers are displayed.-symbol)
2.-(variable left alignment)
3.0 (use 0 as the padding character on the right alignment)
4. [0-9] (set the minimum width of the variable)
5. [0-9] (set the floating point precision or string length)

Copy codeThe Code is as follows:
/**
*
* Javascript sprintf
* Http://www.webtoolkit.info/
*
*
**/

SprintfWrapper = {

Init: function (){

If (typeof arguments = "undefined") {return null ;}
If (arguments. length <1) {return null ;}
If (typeof arguments [0]! = "String") {return null ;}
If (typeof RegExp = "undefined") {return null ;}

Var string = arguments [0];
Var exp = new RegExp (/(% ([%] | (\-)? (\ + | \ X20 )? (0 )? (\ D + )? (\. (\ D )?)? ([BcdfosxX])/g );
Var matches = new Array ();
Var strings = new Array ();
Var convCount = 0;
Var stringPosStart = 0;
Var stringPosEnd = 0;
Var matchPosEnd = 0;
Var newString = '';
Var match = null;

While (match = exp.exe c (string )){
If (match [9]) {convCount + = 1 ;}

StringPosStart = matchPosEnd;
StringPosEnd = exp. lastIndex-match [0]. length;
Strings [strings. length] = string. substring (stringPosStart, stringPosEnd );

MatchPosEnd = exp. lastIndex;
Matches [matches. length] = {
Match: match [0],
Left: match [3]? True: false,
Sign: match [4] | '',
Pad: match [5] | '',
Min: match [6] | 0,
Precision: match [8],
Code: match [9] | '% ',
Negative: parseInt (arguments [convCount]) <0? True: false,
Argument: String (arguments [convCount])
};
}
Strings [strings. length] = string. substring (matchPosEnd );

If (matches. length = 0) {return string ;}
If (arguments. length-1) <convCount) {return null ;}

Var code = null;
Var match = null;
Var I = null;

For (I = 0; I <matches. length; I ++ ){

If (matches [I]. code = '%') {substitution = '% '}
Else if (matches [I]. code = 'B '){
Matches [I]. argument = String (Math. abs (parseInt (matches [I]. argument). toString (2 ));
Substitution = sprintfWrapper. convert (matches [I], true );
}
Else if (matches [I]. code = 'C '){
Matches [I]. argument = String (String. fromCharCode (parseInt (Math. abs (parseInt (matches [I]. argument )))));
Substitution = sprintfWrapper. convert (matches [I], true );
}
Else if (matches [I]. code = 'D '){
Matches [I]. argument = String (Math. abs (parseInt (matches [I]. argument )));
Substitution = sprintfWrapper. convert (matches [I]);
}
Else if (matches [I]. code = 'F '){
Matches [I]. argument = String (Math. abs (parseFloat (matches [I]. argument). toFixed (matches [I]. precision? Matches [I]. precision: 6 ));
Substitution = sprintfWrapper. convert (matches [I]);
}
Else if (matches [I]. code = 'O '){
Matches [I]. argument = String (Math. abs (parseInt (matches [I]. argument). toString (8 ));
Substitution = sprintfWrapper. convert (matches [I]);
}
Else if (matches [I]. code ='s '){
Matches [I]. argument = matches [I]. argument. substring (0, matches [I]. precision? Matches [I]. precision: matches [I]. argument. length)
Substitution = sprintfWrapper. convert (matches [I], true );
}
Else if (matches [I]. code = 'X '){
Matches [I]. argument = String (Math. abs (parseInt (matches [I]. argument). toString (16 ));
Substitution = sprintfWrapper. convert (matches [I]);
}
Else if (matches [I]. code = 'X '){
Matches [I]. argument = String (Math. abs (parseInt (matches [I]. argument). toString (16 ));
Substitution = sprintfWrapper. convert (matches [I]). toUpperCase ();
}
Else {
Substitution = matches [I]. match;
}

NewString + = strings [I];
NewString + = substitution;

}
NewString + = strings [I];

Return newString;

},

Convert: function (match, nosign ){
If (nosign ){
Match. sign = '';
} Else {
Match. sign = match. negative? '-': Match. sign;
}
Var l = match. min-match. argument. length + 1-match. sign. length;
Var pad = new Array (l <0? 0: l). join (match. pad );
If (! Match. left ){
If (match. pad = "0" | nosign ){
Return match. sign + pad + match. argument;
} Else {
Return pad + match. sign + match. argument;
}
} Else {
If (match. pad = "0" | nosign ){
Return match. sign + match. argument + pad. replace (/0/g ,'');
} Else {
Return match. sign + match. argument + pad;
}
}
}
}

Sprintf = sprintfWrapper. init;

If you only want to replace the content of a simple location variable without additional formatting, you can use the printf provided in simple YUI tools:

Copy codeThe Code is as follows:
YAHOO. Tools. printf = function (){
Var num = arguments. length;
Var oStr = arguments [0];
For (var I = 1; I <num; I ++ ){
Var pattern = "\ {" + (I-1) + "\\}";
Var re = new RegExp (pattern, "g ");
OStr = oStr. replace (re, arguments [I]);
}
Return oStr;
}

When used, it is like YAHOO. Tools. printf ("display string {0}, {1 }. "," 1 "," 2 "); Use {?} For matching.

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.