Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

javascript - How do I pad out a string with leading blanks?

I need to test the length of string $A in an UltraEdit script (JavaScript), and then pad it out with leading blanks if it is less than x (say: 30). There's the following suggesting to be found on Stack Overflow, but it doesn't seem to work in an UltraEdit script.

$AAA .= (" " x (35 - length($AAA)));

Suggestions appreciated.

PS: UltraEdit uses JavaScript core engine for scripts.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In JavaScript core used in UltraEdit scripts there is no function to print formatted into a string variable.

But it is nevertheless very easy to create aligned strings with leading spaces or zeros.

Example for fixed length output of a number:

var nNumber = 30;            // number to output right aligned with 4 digits
var sAlignSpaces = "    ";   // string containing the spaces (or zeros) for aligning

// Convert integer number to decimal string.
var sNumber = nNumber.toString(10);
// Has the decimal string less than 4 characters as defined by sAlignSpaces?
if (sNumber.length < sAlignSpaces.length)
{
   // Build decimal string new with X spaces (here 2) from the alignment
   // spaces string and concatenate this string with the number string.
   sNumber = sAlignSpaces.substr(0,sAlignSpaces.length-sNumber.length) + sNumber;
}
// String sNumber has now always at least 4 characters with
// 0 to 3 leading spaces depending on decimal value of the number.

Example for aligning positive numbers dynamically depending on highest number:

var nHighestNumber = 39428;  // highest number usually determined before
var nCurrentNumber = 23;     // current number to output right aligned

// Convert the highest number to a decimal string and get a copy
// of this string with every character replaced by character '0'.
// With highest number being 39428 the created string is "00000".
var sLeadingZeros = nHighestNumber.toString(10).replace(/./g,"0");

// Convert integer number to decimal string.
var sNumber = nCurrentNumber.toString(10);
// Has the decimal string of the current number less
// characters than the decimal string of the highest number?
if (sNumber.length < sLeadingZeros.length)
{
   // Build decimal string new with X (here 3) zeros from the alignment
   // string and concatenate this leading zero string with the number string.
   sNumber = sLeadingZeros.substr(0,sLeadingZeros.length-sNumber.length) + sNumber;
}
// String sNumber has now always at least 5 characters with 0 to 4
// leading zeros depending on decimal value of the positive number.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...