#68 字符串居中补全


  • 0
    administrators

    完成函数 centerPad 可以让一个字符串居中包裹在指定的可重复填充的字符串中间,例如:

    centerPad('Hello', 13, 'abc') // => 'abcaHelloabca'
    centerPad('Gook Luck!', 30, '*~') // => '*~*~*~*~*~Gook Luck!*~*~*~*~*~'
    

    第一个参数为被包裹字符串,第二个参数为最终的字符串长度,第三个参数为用来填充的字符。

    如果字符串无法完全居中,那么让字符串偏左,例如:

    centerPad('Hello', 10, 'abc') // => 'abHelloabc'
    

    如果第二个参数传入的字符串长度比原来长度要短,直接返回原有字符串即可,例如:

    centerPad('Hello', 1, 'abc') // => 'Hello'
    

    请你完成 centerPad 函数。

    (提示:可以充分利用 ES6 新增的扩展方法)


  • 0

    const centerPad = (str, len, pad) => {
    let strLen = str.length,
    half = (len - strLen) / 2,
    start = str.padStart(half + strLen, pad)
    return (len - strLen) % 2 != 0 ? start + ''.padEnd(half + 1, pad) : start + ''.padEnd(half, pad)
    }


  • 1

    const centerPad = (str, len, pad) => str.padStart(~~((len - str.length) / 2) + str.length, pad).padEnd(len, pad)
    

    前端表单验证工具 https://github.com/WLDragon/SMValidator


  • 0

    每次都写的很多,勉强写出来了,新手没办法呢

    const centerPad = (str, len, pad) => {
       if (str.length >= len || pad === "") {
           return str;
       }
    
       let str_left = "",
           str_right = "";
    
       var num = 0, str1 = pad;
       while (num < parseInt((len - str.length) / 2)) {
           str_left = str_left + str1[num];
           str_right = str_right + str1[num];
           num++;
           if (num >= str1.length) {
               str1 = str1 + pad;
           }
       }
    
       if ((len - str.length) % 2 !== 0) {
           str_right = str_right + str1[num];
       }
    
       return str_left + str + str_right;
    
    }

  • 0

    才发现有padStart, padEnd 这种好东西!

    const centerPad = (str, len, pad) => {
    	if (len <= str.length || pad.length < 1) {
    		return str;
    	}
    
    	let strStart = Math.floor((len - str.length) / 2);
    	let padMaxLength = strStart + 1;
    	pad = pad.repeat(Math.floor(padMaxLength / pad.length) + 1);
    
    
    	return pad.slice(0, strStart) + str + pad.slice(0, len - strStart - str.length);
    }
    

  • 0

    const centerPad = (str, len, pad) => {
    let length = str.length
    let fillLen = len - length
    let leftLen = 0
    let rightLen = 0
    if(len < length){
    return str
    }
    if (fillLen % 2 === 1) {
    leftLen = Math.floor(fillLen / 2)
    rightLen = leftLen + 1
    } else {
    leftLen = fillLen / 2
    rightLen = leftLen
    }
    leftStr = pad.padEnd(leftLen, pad)
    leftStr = leftStr.slice(0, leftLen)
    rightStr = pad.padEnd(rightLen, pad)
    rightStr = rightStr.slice(0, rightLen)
    str = leftStr + str + rightStr
    return str
    }


  • 0

    const centerPad = (str, len, pad) => {
        var overlen;
        if(len%2!=0&&str.length%2==0) {
            overlen = Math.floor(str.length/2)-1;
        } else {
            overlen = Math.floor(str.length/2);
        }
        var middle = Math.floor((len+1)/2);
        var needlen = middle+overlen;
        str = str.padStart(needlen,pad);
        str = str.padEnd(len,pad);
        return str;
    }
    
    

  • 0

    const centerPad = (str, len, pad) => {
      const length = str.length;
      return len <= length ? str :
        str.padStart(Math.floor((len - length) / 2) + length, pad).padEnd(len, pad);
    }
    

  • 0

    const centerPad = (str, len, pad) => {
        return str.padStart(Math.floor((len-str.length)/2) + str.length,pad)
                 .padEnd(len,pad);
    }
    

  • 0

    const centerPad = (str, len, pad) =>{
    var _strLen = str.length;
    var residueLen = len - _strLen;
    if(residueLen>0){
    str = str.padEnd(_strLen+((residueLen/2).toFixed(0)-0),pad)
    .padStart(len,pad);
    }
    return str
    }


登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待