How to Implement the String Repeat Function in Javascript?

  • 时间:2020-10-05 13:15:44
  • 分类:网络文摘
  • 阅读:149 次

The string repeat function namely, String.prototype.repeat() is available since ECMAScript 6. The String Repeat function is a handy string method that allows you to repeat a source string a number of times, which is generally available in many dynamic programming languages, such as Python or Magik.

For example, in Python or Magik, you can do this to repeat the given string e.g. hello 3 times – “hellohellohello”

1
"hello" * 3
"hello" * 3

Earlier than ES6, you can easily implement a string repeat function in Javascript – which will be a good coding exercise to get familiar with Javascript.

String Repeat Using Array and Join

In Javascript, the Arrays are generally faster than strings. We can construct an array of given size, fill them with the source string, and finally join them by “” (which is thus the concatenation of N strings)

1
2
3
String.prototype.repeat = function(n) {
    return Array(n).fill(this).join('');
}
String.prototype.repeat = function(n) {
    return Array(n).fill(this).join('');
}

Alternatively, we can use Array.from which takes two parameters, the first will be a json and the second will be a lambda function:

1
2
3
String.prototype.repeat = function(n) {
    return Array.from({length: n}, () => this).join('');
}
String.prototype.repeat = function(n) {
    return Array.from({length: n}, () => this).join('');
}

String Repeat Using Loops

Simple loops are always straightforward.

1
2
3
4
5
6
7
String.prototype.repeat = function(n) {
    let s = "";
    for (let i = 0; i < n; ++ i) {
        s += this;
    }
    return s;
}
String.prototype.repeat = function(n) {
    let s = "";
    for (let i = 0; i < n; ++ i) {
        s += this;
    }
    return s;
}

Using a while loop may seem cleaner:

1
2
3
4
5
6
7
String.prototype.repeat = function(n) {
    let s = "";
    while (n -- > 0) {
        s += this;
    }
    return s;
}
String.prototype.repeat = function(n) {
    let s = "";
    while (n -- > 0) {
        s += this;
    }
    return s;
}

String repeat method implemented in Recursion

Using recursion, no problem. The following tail recursion implementation may in general be optimised away by the JS compiler/interpreter.

1
2
3
String.prototype.repeat = function(n) {
    return n > 0 ? this + this.repeat(n - 1) : "";
}
String.prototype.repeat = function(n) {
    return n > 0 ? this + this.repeat(n - 1) : "";
}

Reducing the Concatenation Times

We may reduce the number of string concatenation by doubling the source template i.e. for example, “a” * 5 = (“a” * 2) * 2 + “a” * 1. In this example, we do 3 concatenations instead of 5.

1
2
3
4
5
6
7
8
9
10
11
12
String.prototype.repeat = function(n) {
    if (n < 1) return "";
    let s = "";
    let x = this;
    while (n > 1) {
        if (n & 1) s += x;  // deal with the odd number
        x += x;  // doubling the source template
        n >>= 1;   // reducing the complexity to O(logN)
    }
    s += x; // we still have n=1 times
    return s;
}
String.prototype.repeat = function(n) {
    if (n < 1) return "";
    let s = "";
    let x = this;
    while (n > 1) {
        if (n & 1) s += x;  // deal with the odd number
        x += x;  // doubling the source template
        n >>= 1;   // reducing the complexity to O(logN)
    }
    s += x; // we still have n=1 times
    return s;
}

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
中华人民共和国反家庭暴力法(主席令第三十七号)  全国人大常委会关于修改《中华人民共和国教育法》的决定(主席令第三十九号)  中华人民共和国国家勋章和国家荣誉称号法(主席令第三十八号)  中华人民共和国反恐怖主义法(主席令第三十六号)  地图管理条例(国务院令第664号)   中华人民共和国宪法  全国社会保障基金条例(国务院令第667号)   2016年国务院关于修改部分行政法规的决定  居住证暂行条例(国务院令第663号)   国务院关于修改《建设工程勘察设计管理条例》的决定(国务院令第662号)  
评论列表
添加评论