Insert a string at a specific index

前端 未结 18 869
眼角桃花
眼角桃花 2020-11-22 14:02

How can I insert a string at a specific index of another string?

 var txt1 = \"foo baz\"

Suppose I want to insert \"bar \" after the \"foo

18条回答
  •  温柔的废话
    2020-11-22 14:25

    I wanted to compare the method using substring and the method using slice from Base33 and user113716 respectively, to do that I wrote some code

    also have a look at this performance comparison, substring, slice

    The code I used creates huge strings and inserts the string "bar " multiple times into the huge string

    if (!String.prototype.splice) {
        /**
         * {JSDoc}
         *
         * The splice() method changes the content of a string by removing a range of
         * characters and/or adding new characters.
         *
         * @this {String}
         * @param {number} start Index at which to start changing the string.
         * @param {number} delCount An integer indicating the number of old chars to remove.
         * @param {string} newSubStr The String that is spliced in.
         * @return {string} A new string with the spliced substring.
         */
        String.prototype.splice = function (start, delCount, newSubStr) {
            return this.slice(0, start) + newSubStr + this.slice(start + Math.abs(delCount));
        };
    }
    
    String.prototype.splice = function (idx, rem, str) {
        return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));
    };
    
    
    String.prototype.insert = function (index, string) {
        if (index > 0)
            return this.substring(0, index) + string + this.substring(index, this.length);
    
        return string + this;
    };
    
    
    function createString(size) {
        var s = ""
        for (var i = 0; i < size; i++) {
            s += "Some String "
        }
        return s
    }
    
    
    function testSubStringPerformance(str, times) {
        for (var i = 0; i < times; i++)
            str.insert(4, "bar ")
    }
    
    function testSpliceStringPerformance(str, times) {
        for (var i = 0; i < times; i++)
            str.splice(4, 0, "bar ")
    }
    
    
    function doTests(repeatMax, sSizeMax) {
        n = 1000
        sSize = 1000
        for (var i = 1; i <= repeatMax; i++) {
            var repeatTimes = n * (10 * i)
            for (var j = 1; j <= sSizeMax; j++) {
                var actualStringSize = sSize *  (10 * j)
                var s1 = createString(actualStringSize)
                var s2 = createString(actualStringSize)
                var start = performance.now()
                testSubStringPerformance(s1, repeatTimes)
                var end = performance.now()
                var subStrPerf = end - start
    
                start = performance.now()
                testSpliceStringPerformance(s2, repeatTimes)
                end = performance.now()
                var splicePerf = end - start
    
                console.log(
                    "string size           =", "Some String ".length * actualStringSize, "\n",
                    "repeat count          = ", repeatTimes, "\n",
                    "splice performance    = ", splicePerf, "\n",
                    "substring performance = ", subStrPerf, "\n",
                    "difference = ", splicePerf - subStrPerf  // + = splice is faster, - = subStr is faster
                    )
    
            }
        }
    }
    
    doTests(1, 100)

    The general difference in performance is marginal at best and both methods work just fine (even on strings of length ~~ 12000000)

提交回复
热议问题