Does JavaScript have a built in stringbuilder class?

前端 未结 10 1610
Happy的楠姐
Happy的楠姐 2020-11-30 19:33

I see a few code project solutions.

But is there a regular implementation in JavaScript?

相关标签:
10条回答
  • 2020-11-30 20:32

    In C# you can do something like

     String.Format("hello {0}, your age is {1}.",  "John",  29) 
    

    In JavaScript you could do something like

     var x = "hello {0}, your age is {1}";
     x = x.replace(/\{0\}/g, "John");
     x = x.replace(/\{1\}/g, 29);
    
    0 讨论(0)
  • 2020-11-30 20:33

    For those interested, here's an alternative to invoking Array.join:

    var arrayOfStrings = ['foo', 'bar'];
    var result = String.concat.apply(null, arrayOfStrings);
    console.log(result);
    

    The output, as expected, is the string 'foobar'. In Firefox, this approach outperforms Array.join but is outperformed by + concatenation. Since String.concat requires each segment to be specified as a separate argument, the caller is limited by any argument count limit imposed by the executing JavaScript engine. Take a look at the documentation of Function.prototype.apply() for more information.

    0 讨论(0)
  • 2020-11-30 20:34

    How about sys.StringBuilder() try the following article.

    https://msdn.microsoft.com/en-us/library/bb310852.aspx

    0 讨论(0)
  • 2020-11-30 20:37

    The ECMAScript 6 version (aka ECMAScript 2015) of JavaScript introduced string literals.

    var classType = "stringbuilder";
    var q = `Does JavaScript have a built-in ${classType} class?`;
    

    Notice that back-ticks, instead of single quotes, enclose the string.

    0 讨论(0)
提交回复
热议问题