Create a string of variable length, filled with a repeated character

前端 未结 10 1120
刺人心
刺人心 2020-11-28 03:59

So, my question has been asked by someone else in it\'s Java form here: Java - Create a new String instance with specified length and filled with specific character. Best so

10条回答
  •  生来不讨喜
    2020-11-28 04:34

    You can use the first line of the function as a one-liner if you like:

    function repeat(str, len) {
        while (str.length < len) str += str.substr(0, len-str.length);
        return str;
    }
    

提交回复
热议问题