#68 字符串居中补全


  • 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
    }


  • 0

    这个才对

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

  • 0

    const repeat = (s, n) => {
      return !n ? '' : s + repeat(s, n - 1)
    }
    
    const centerPad = (str, len, pad) => {
      const l = str.length
    
      if (len <= l) {
        return str
      }
    
      const t = pad.length
    
      const lt = ~~((len - l) / 2)
      const rt = len - l - lt
    
      return repeat(pad, ~~(lt / t)) + pad.slice(0, lt % t) + str + repeat(pad, ~~(rt / t)) + pad.slice(0, rt % t)
    
    }
    
    

  • 0

    /**
     * 长度判断可有可无,因为padStart自带长度判断了
     */
    const centerPad = (str, len, pad) => /* TODO */{
      if(str.length >= len){
        return str
      } else {
        let half = Math.floor((len-str.length) / 2);
        str = str.padStart(half+str.length, pad);
        return str.padEnd(len, pad);
      }
    }
    

  • 0

    const centerPad = (str, len, pad) => {
      // 如果第二个参数传入的字符串长度比原来长度要短,直接返回原有字符串即可
      if (len < str.length) return str
    
      const start = Math.floor((len - str.length) / 2)
      return str.padStart(start + str.length, pad).padEnd(len, pad)
    }
    
    console.log(centerPad('Gook Luck!', 30, '*~'))
    console.log(centerPad('Hello', 10, 'abc'))
    

  • 0

    使用padStart:

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

    不使用:

    const centerPad = (str, len, pad) => {
      if (str.length >= len) {
        return str
      }
      const restTotal = len - str.length;
      const restRight = Array.from(new Array(Math.ceil(restTotal / 2)), (v, index) => {
        let strIndex = index >= pad.length ? index % pad.length : index;
        return pad[strIndex]
      });
      const restLeft = Array.from(new Array(Math.floor(restTotal / 2)), (v, index) => {
        let strIndex = index >= pad.length ? index % pad.length : index;
        return pad[strIndex]
      });
      return [...restLeft, str, ...restRight].join('')
    };
    

  • 0

    const centerPad = (str, len, pad) => {
      if(len <= str.length) return str;
      let left = Math.floor((len - str.length)/2);
      let right = len - str.length - left;
      
      return func(left, pad) + str + func(right, pad)
    }
    
    function func(num, str) {
      return str.repeat(Math.floor(num/str.length)) + str.slice(0,num%str.length);
    } 
    

    上述代码不通过,不知道哪里出错了?


登录后回复
 

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