/**** Javascript sprintf* http://www.webtoolkit.info/**%%–returns A percent sign*%b–binary number*%c–the Cha Racter according to the ASCII value*%d–signed decimal number*%f–floating-point number*%o–octal number*%s–s tring*%x–hexadecimal Number (lowercase letters) *%x–hexadecimal number (uppercase letters) ***/sprintfwrapper = {ini T: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.exec (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 = = '% ') {Substitut Ion = '% '}else if (Matches[i].code = = ' B ') {matches[i].argument = String (Math.Abs (parseint)). 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); 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.si GN = 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); (!match.left) {if (Match.pad = = "0" | | nosign) {return match.sign + pad + match.argument;} else {return pad + match.sign + match.argumen t;}} else {if (Match.pad = = "0" | | nosign) {return match.sign + match.argument + pad.replace (/0/g, ');} else {return match.si GN + match.argument + Pad;}}} sprintf = Sprintfwrapper.init;
The sprintf of JavaScript