Append Method (Char[], Int32, Int32)

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

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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)

'Declaration
Public Function Append ( _
	value As Char(), _
	startIndex As Integer, _
	charCount As Integer _
) As StringBuilder

Parameters

value
Type: System.Char ()
A character array.
startIndex
Type: System.Int32
The starting position in value.
charCount
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.

ExceptionCondition
ArgumentNullException

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

ArgumentOutOfRangeException

charCount is less than zero.

-or-

startIndex is less than zero.

-or-

startIndex + charCount is less than the length of value.

-or-

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() + Environment.NewLine
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.Append(xUInt16).Append(sep)
      sb.Append(xUInt32).Append(sep).Append(xUInt64).Append(sep)
      sb.Append(xSByte)
      '
      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--->>>>
'


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft