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


  • 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('')
    };
    

登录后回复
 

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