Sys.StringBuilder Class
This page is specific to:.NET Framework Version:3.54.0
Sys.StringBuilder Class

Provides a mechanism to concatenate strings.

Namespace: Sys

Inherits: None

var stringBuilderVar = StringBuilder(string);
Constructors

Sys.StringBuilder Constructor

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

Members

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.

Remarks

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

Example

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.

Imports System.Text

Public Module App 
    Public Sub Main() 
        ' Create a StringBuilder that expects to hold 50 characters.
        ' Initialize the StringBuilder with "ABC".
        Dim sb As New StringBuilder("ABC", 50)

        ' Append three characters (D, E, and F) to the end of the StringBuilder.
        sb.Append(New Char() {"D"c, "E"c, "F"c})

        ' Append a format string to the end of the StringBuilder.
        sb.AppendFormat("GHI{0}{1}", "J"c, "k"c)

        ' Display the number of characters in the StringBuilder and its string.
        Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString())

        ' Insert a string at the beginning of the StringBuilder.
        sb.Insert(0, "Alphabet: ")

        ' Replace all lowercase k's with uppercase K's.
        sb.Replace("k", "K")

        ' Display the number of characters in the StringBuilder and its string.
        Console.WriteLine("{0} chars: {1}", sb.Length, sb.ToString())
    End Sub
End Module

' This code produces the following output.
'
' 11 chars: ABCDEFGHIJk
' 21 chars: Alphabet: ABCDEFGHIJK


See Also

Reference

Other Resources

Community Content

Incorrect sample code
Added by:JChung2007
The ASP.NET AJAX client-side script reference should not have C#, Visual Basic, or C++ samples.
JScript sample code incorrect
Added by:M Derrick Glass

In the JScript example, the two lines here:

...
var sb = new Sys.StringBuilder(this._headTagStart);
sb.append(titleTagEnd);

should be:

var sb = new Sys.StringBuilder(headTagStart);
sb.append(titleTagStart);

Also, while the description of the expected result in the JScript sample is correct, the last statement in the Example documentation is mostly incorrect:

Finally, the code calls the toString method, passing a "|" character as a delimiter to insert between the elements of the string returned by StringBuilder.

© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View