我需要在UltraEdit脚本(JavaScript)中测试字符串$ A的长度,如果它小于x(比如说:30),则用前导空白填充它。有以下建议在堆栈溢出中找到,但它似乎不能在UltraEdit脚本中工作。如何用前导空白填充字符串?
$AAA .= (" " x (35 - length($AAA)));
建议感激。
PS:UltraEdit使用脚本的JavaScript核心引擎。
我需要在UltraEdit脚本(JavaScript)中测试字符串$ A的长度,如果它小于x(比如说:30),则用前导空白填充它。有以下建议在堆栈溢出中找到,但它似乎不能在UltraEdit脚本中工作。如何用前导空白填充字符串?
$AAA .= (" " x (35 - length($AAA)));
建议感激。
PS:UltraEdit使用脚本的JavaScript核心引擎。
我想你正在寻找的是这样的:
在在UltraEdit脚本中使用JavaScript的核心没有要打印格式化成一个字符串变量函数。
但是,使用前导空格或零来创建对齐的字符串非常容易。
实施例为多个的固定长度的输出:
用于对准动态依赖于最高数目的正数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.
实施例:
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.
谢谢。麻烦的是:Javascript似乎没有这个命令。 – fvg 2013-05-07 10:03:56
啊,我明白了。我不知道我是如何看错你的问题的。对于那个很抱歉。您可以尝试创建一个需要预先添加到字符串的空格数组,并将现有的字符串添加为最后一个元素,然后在数组上执行.join('')。它不是最干净的,但它可能工作。 – 2013-05-08 11:14:19