Append Method (Char[], Int32, Int32)

StringBuilder.Append Method (Char(), Int32, Int32)

Updated: January 2011

Appends the string representation of a specified subarray of Unicode characters to the end of this instance.

Namespace:  System.Text
Assembly:  mscorlib (in mscorlib.dll)

<SecuritySafeCriticalAttribute> _
Public Function Append ( _
	value As Char(), _
	startIndex As Integer, _
	charCount As Integer _
) As StringBuilder


Type: System.Char()
A character array.
Type: System.Int32
The starting position in value.
Type: System.Int32
The number of characters to append.

Return Value

Type: System.Text.StringBuilder
A reference to this instance after the append operation has completed.


value is Nothing, and startIndex and charCount are not zero.


charCount is less than zero.


startIndex is less than zero.


startIndex + charCount is less than the length of value.


Enlarging the value of this instance would exceed its maximum capacity.

This method appends the specified range of characters in value to the current instance. If value is Nothing and startIndex and count are both zero, no changes are made.

The Append method modifies the existing instance of this class; it does not return a new class instance. Because of this, you can call a method or property on the existing reference and you do not have to assign the return value to a StringBuilder object, as the following example illustrates.

Dim chars() As Char = { "a"c, "b"c, "c"c, "d"c, "e"c}
Dim sb As New System.Text.StringBuilder()
Dim startPosition As Integer = Array.IndexOf(chars, "a"c)
Dim endPosition As Integer = Array.IndexOf(chars, "c"c)
If startPosition >= 0 AndAlso endPosition >= 0 Then
   sb.Append("The array from positions ").Append(startPosition).
             Append(" to ").Append(endPosition).Append(" contains ").
             Append(chars, startPosition, endPosition + 1).Append(".")
   outputBlock.Text += sb.ToString() + vbCrLf
End If             
' The example displays the following output:
'       The array from positions 0 to 2 contains abc.

The capacity of this instance is adjusted as needed.

The following example demonstrates how to append various data type values to a StringBuilder object.

Imports System.Text

Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim sep As String = ", "
      Dim head As String = "<<<"
      Dim tail As Char() = {">"c, ">"c, ">"c}
      Dim dash As Char = "-"c
      Dim obj As [Object] = 0

      Dim xBool As Boolean = True
      Dim xByte As Byte = 1
      Dim xInt16 As Short = 2
      Dim xInt32 As Integer = 3
      Dim xInt64 As Long = 4
      Dim xDecimal As [Decimal] = 5
      Dim xSingle As Single = 6.6F
      Dim xDouble As Double = 7.7

      ' The following types are not CLS-compliant.
      Dim xUInt16 As UShort = 8
      Dim xUInt32 As UInteger = 9
      Dim xUInt64 As ULong = 10
      Dim xSByte As SByte = -11
      Dim sb As New StringBuilder()

      sb = sb.Append(head)              ' <<<
      sb = sb.Append(head, 2, 1)        ' <<<<
      sb = sb.Append(dash)              ' <<<<-
      sb = sb.Append(dash).Append(dash) ' <<<<---
      sb = sb.Append(xBool).Append(sep)
      sb = sb.Append(obj).Append(sep).Append(xByte).Append(sep)
      sb = sb.Append(xInt16)
      sb = sb.Append(sep)
      sb = sb.Append(xInt32)
      sb = sb.Append(sep)
      sb = sb.Append(xInt64)
      sb = sb.Append(sep)
      sb = sb.Append(xDecimal).Append(sep)
      sb = sb.Append(xSingle).Append(sep).Append(xDouble)

      ' The following Append methods are not CLS-compliant.
      sb = sb.Append(dash, 3)           ' ---
      sb = sb.Append(tail)              ' --->>>
      sb = sb.Append(tail, 2, 1)        ' --->>>>
      Dim str As [String] = sb.ToString()
      outputBlock.Text &= "The appended string is:" & vbCrLf
      outputBlock.Text &= str & vbCrLf
   End Sub 'Main
End Class 'Sample
'This example produces the following results:
'The appended string is:
'<<<<---True, 0, 1, 2, 3, 4, 5, 6.6, 7.78, 9, 10, -11--->>>>


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.




January 2011

Expanded the Remarks section and removed the example.

Customer feedback.

Community Additions

© 2016 Microsoft