Export (0) Print
Expand All

Sys.StringBuilder Class

Provides a mechanism to concatenate strings.

Namespace: Sys

Inherits: None

var stringBuilderVar = StringBuilder(string);

Sys.StringBuilder Constructor

Creates a new instance of StringBuilder and optionally accepts initial text to concatenate.

Sys.StringBuilder append Method

Appends a string to the end of the StringBuilder instance.

Sys.StringBuilder appendLine Method

Appends a new string with a line terminator to the end of the StringBuilder instance.

Sys.StringBuilder clear Method

Clears the contents of the StringBuilder instance.

Sys.StringBuilder isEmpty Method

Determines whether the StringBuilder instance has any content.

Sys.StringBuilder toString Method

Creates a string from the contents of a StringBuilder instance.

The StringBuilder class represents a mutable string of characters and provides a mechanism to concatenate a sequence of strings.

The following example shows how to create a new StringBuilder instance and call the append method to add a line of text. The code then calls the appendLine method to add a string with a line terminator at the end of the string. Finally, the code calls the toString method, passing a "|" character as a delimiter to insert between the elements of the string returned by StringBuilder.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Samples</title>
</head>
<body>
    <form id="form1" runat="server">
       <asp:ScriptManager runat="server" ID="ScriptManager1">
       </asp:ScriptManager>

       <script type="text/javascript">
            function buildAString(title){
                var headTagStart = "<head>";
                var headTagEnd = "</head>";
                var titleTagStart = "<title>";
                var titleTagEnd = "</title>";

                var sb = new Sys.StringBuilder(this._headTagStart);
                sb.append(titleTagEnd);
                sb.append(title);
                sb.append(titleTagEnd);
                sb.append(headTagEnd);
                // Displays: "The result: <head><title>A Title</title></head>"
                alert("The result" + sb.toString()); 
            }

            var title = "A Title";
            buildAString(title);

        </script>
    </form>
</body>
</html>



Community Additions

ADD
Show:
© 2014 Microsoft