Sprintf Like Utilities For JScript |
|
| |
| |
This code can be used to produce formated strings. Mostly useful for WSH and text areas displayed with fixed fonts. |
| |
|
| |
// sprintf realization
// Samples:
// WScript.Echo("%15l%10r%10r%10r%10r".format(new Array("NAME","TYPE","LENGTH","PRECISION","SCALE")) );
// or using the sprintf
// WScript.Echo(sprintf("%15l%10r%10r%10r%10r","NAME","TYPE","LENGTH","PRECISION","SCALE");
// Format description:
// Use format string and attached format function.
// Pass to it array of printed arguments
// or use the sprintf helper function.
// Format specifiers are:
// %[width]align
// width - optional field width
// align - l or r - specifies text alignment
String.prototype.formatLength = function (n,left) {
var str = "";
var i;
if (this.length < n) {
if (left) {
str += this;
for (i = 0; i < (n - this.length); i ++) str += " ";
} else {
for (i = 0; i < (n - this.length); i ++) str += " ";
str += this;
}
} else {
if (left) {
str = this.slice(this.length-n-1,-1);
} else {
str = this.slice(0,n-1);
}
}
return str;
}
String.prototype.format = function(arrArgs) {
var fmt = this;
var re = /(%d*[lr]{1})/gi;
var reNum = /d+/gi;
var fmtParts = fmt. split(re );
var fmtArgs = fmt.match(re);
var nP = 0, nA = 0,nAFirst = 0;
var str = "";
var fmtArgWidth;
var Width;
var strTemp;
if (fmtParts == null) fmtParts = new Array();
if (fmtArgs == null) fmtArgs = new Array();
if (fmt.charAt(0) == "%") nAFirst = 1;
while (nP < fmtArgs.length || nA < fmtParts.length) {
if (nA < (nP + nAFirst)) {
if (nA < fmtArgs.length) {
if (nA < arrArgs.length) {
fmtArgWidth = fmtArgs[nA].match(reNum);
if (fmtArgWidth != null) Width = new Number(fmtArgWidth[0]);
else Width = 0;
strTemp = new String(arrArgs[nA]);
if (fmtArgs[nA].charAt(fmtArgs[nA].length - 1) == "l") {
str += strTemp.formatLength(Width,true);
} else {
str += strTemp.formatLength(Width,false);
}
}
}
nA ++;
} else {
if (nP < fmtParts.length) {
str += fmtParts[nP];
}
nP ++;
}
}
return str;
}
// Helper functions - easy to use but a bit slower
for (i = 1; i < sprintf.arguments.length; i++ ) {
arr [arr.length ] = sprintf.arguments [i ];
}
return str.format(arr);
}
// This one is for WSH only - remove it if gonna use the script in a different environment
for (i = 1; i < sprintf.arguments.length; i++ ) {
arr [arr.length ] = sprintf.arguments [i ];
}
WScript. Echo(str.format (arr ));
}
|
|
| |
|
|
| |
Date entered :
17th Feb 2001
Rating : No Rating
Accessed :
1129
Submitted by :
micle |
|
|
| |
|
|
| |
| |
|
Click each image to add this page to each site. |
|
|
| |
| |
|
| |
|
|
No comment available at the moment.Be the first one to make a comment
|
|
|
| |
|
|
| Related JavaScript Source Codes |
|
Database error: Invalid SQL: select FLOOR( AVG(b.rating)) as avg_rating,a.sourcecode_id sourcecode_id ,title,description,author,category_id,skill_level,reference,reference_mail,reference_url,a.user_id user_id,date_entered,source,accessed,dev_id,file_url,total_comments,instruction,registered,authorised,fld_deletemark,fld_code_version,fld_searchkeyword,fld_code_status,category_id from tbl_sourcecode a LEFT JOIN tbl_sourcecodecomment b ON a.sourcecode_id = b.sourcecode_id LEFT JOIN tbl_codecategory c ON a.category_id = c.fld_categoryID where a.fld_deletemark = 'N' AND a.fld_enable_disable= 'Y' AND a.authorised='Y' AND c.fld_groupIDFK='6' AND a.sourcecode_id!='247' GROUP BY a.sourcecode_id order by a.sourcecode_id DESC limit 0, 5
MySQL Error: 1030 (Got error 122 from storage engine)
Session halted.
Warning: Unknown(): write failed: Disk quota exceeded (122) in Unknown on line 0
Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0
| |