我需要一个JavaScript函数,它可以取一个值,并将其填充到给定的长度(我需要空格,但任何事情都会做)。我发现了这个,但我不知道它在做什么,它似乎对我不起作用。

String.prototype.pad = function(l, s, t) { return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length) + 1).join(s)) .substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2)) + this + s.substr(0, l - t) : this; }; var s = "Jonas"; document.write( '<h2>S = '.bold(), s, "</h2>", 'S.pad(20, "[]", 0) = '.bold(), s.pad(20, "[]", 0), "<br />", 'S.pad(20, "[====]", 1) = '.bold(), s.pad(20, "[====]", 1), "<br />", 'S.pad(20, "~", 2) = '.bold(), s.pad(20, "~", 2) );


当前回答

在ES8中,有两个填充选项。

您可以在文档中查看它们。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart

其他回答

捷径:

(x=>(new Array(int-x.length+1)).join(char)+x)(String)

例子:

(x=>(new Array(6-x.length+1)).join("0")+x)("1234")

返回:“001234”

下面是一个JavaScript函数,它使用自定义符号添加指定数量的填充。该函数接受三个参数。

padMe --> string or number to left pad
pads  --> number of pads
padSymble --> custom symbol, default is "0"
function leftPad(padMe, pads, padSymble) {
    if(typeof padMe === "undefined") {
        padMe = "";
    }
    if (typeof pads === "undefined") {
        pads = 0;
    }
    if (typeof padSymble === "undefined") {
        padSymble = "0";
    }

    var symble = "";
    var result = [];
    for(var i=0; i < pads; i++) {
       symble += padSymble;
    }
    var length = symble.length - padMe.toString().length;
    result = symble.substring(0, length);
    return result.concat(padMe.toString());
}

以下是一些结果:

> leftPad(1)
"1"

> leftPad(1, 4)
"0001"

> leftPad(1, 4, "0")
"0001"

> leftPad(1, 4, "@")
"@@@1"

我喜欢这样做,以防你需要填充多个字符或标签(例如&nbsp;)来显示:

$.padStringLeft = function(s, pad, len) {
    if(typeof s !== 'undefined') {
        var c=s.length; while(len > c) {s=pad+s;c++;}
    }
    return s;
}    

$.padStringRight = function(s, pad, len) {
    if(typeof s !== 'undefined') {
        var c=s.length; while(len > c) {s += pad;c++;}
    }
    return s;
}

像PHP:

const STR_PAD_RIGHT = 1;
const STR_PAD_LEFT = 0;
const STR_PAD_BOTH = 2;

/**
 * @see http://php.net/str_pad
 * @param mixed input 
 * @param integer length 
 * @param string string 
 * @param integer type 
 * @return string
 */
function str_pad(input, length, string, type) {
    if (type === undefined || (type !== STR_PAD_LEFT && type !== STR_PAD_BOTH)) {
        type = STR_PAD_RIGHT
    }

    if (input.toString().length >= length) {
         return input;
    } else {
        if (type === STR_PAD_BOTH) {
            input = (string + input + string);
        } else if (type == STR_PAD_LEFT) {
            input = (string + input);
        } else {
            input = (input + string);
        }

        return str_pad(input.toString(), length, string, type);
    }
}

函数 var _padLeft = function(paddingString, width, replacementChar) { paddingString返回。长度>=宽度?paddingString: _padLeft(replacementChar + paddingString, width, replacementChar || ' '); }; 字符串的原型 String.prototype.padLeft = function(width, replacementChar) { 返回。长度>=宽度?this. tostring ():(replacementChar + this)。padLeft(width, replacementChar || ' '); }; 片 ('00000' + paddingString).slice(-5)