Javascript StringBuilder for Beginners (ASP.NET 3.5 and above)





5.00/5 (1 vote)
Just a simple note with sys.StringBuilder which is just beginner level
Just a beginner tip. Many programmers still use
string
concatenations in the classical way. An example situation like this - Adding and deleting div
dynamically in JavaScript[^]
Are JavaScript string
s mutable? NO. So what is the best way to concatenate string
s in JavaScript like runtime markup generation, etc? The .NET way is to use StringBuilder
. From Framework 3.5, it is available through the Microsoft Ajax Library. Enable Ajax in the page by using a ScriptManager
. Now the Sys.StringBuilder
class is available for use.
<asp:ScriptManager ID="script1" runat ="server">
</asp:ScriptManager>
In Javascript:
var sb = new Sys.StringBuilder();
sb.append("Hello ");
sb.append("World ");
alert(sb.toString());
How does a string
builder do concatenation efficiently? It uses an array
to hold the string
s, then uses the Array.Join
to concatenate the string
s. So intermediate string
generations are avoided.