다음을 통해 공유


StringBuilder.Append Method (array<Char[], Int32, Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function Append ( _
    value As Char(), _
    startIndex As Integer, _
    charCount As Integer _
) As StringBuilder
[SecuritySafeCriticalAttribute]
public StringBuilder Append(
    char[] value,
    int startIndex,
    int charCount
)

Parameters

  • 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.

Exceptions

Exception Condition
ArgumentNullException

value is nulla null reference (Nothing in Visual Basic), 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.

Remarks

This method appends the specified range of characters in value to the current instance. If value is nulla null reference (Nothing in Visual Basic) 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.
char[] chars = { 'a', 'b', 'c', 'd', 'e'};
System.Text.StringBuilder sb = new System.Text.StringBuilder();
int startPosition = Array.IndexOf(chars, 'a');
int endPosition = Array.IndexOf(chars, 'c');
if (startPosition >= 0 && endPosition >= 0) {
   sb.Append("The array from positions ").Append(startPosition).
             Append(" to ").Append(endPosition).Append(" contains ").
             Append(chars, startPosition, endPosition + 1).Append(".");
   outputBlock.Text += sb + Environment.NewLine;
}             
// The example displays the following output:
//       The array from positions 0 to 2 contains abc.

The capacity of this instance is adjusted as needed.

Examples

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--->>>>
'
using System;
using System.Text;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string sep = ", ";
      string head = "<<<";
      char[] tail = { '>', '>', '>' };
      char dash = '-';
      Object obj = 0;

      bool xBool = true;
      byte xByte = 1;
      short xInt16 = 2;
      int xInt32 = 3;
      long xInt64 = 4;
      Decimal xDecimal = 5;
      float xSingle = 6.6F;
      double xDouble = 7.7;

      // The following types are not CLS-compliant.
      ushort xUInt16 = 8;
      uint xUInt32 = 9;
      ulong xUInt64 = 10;
      sbyte xSByte = -11;
      //
      StringBuilder sb = 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).Append(sep);

      // The following Append methods are not CLS-compliant.
      sb = sb.Append(xUInt16).Append(sep);
      sb = sb.Append(xUInt32).Append(sep).Append(xUInt64).Append(sep);
      sb = sb.Append(xSByte);
      //
      sb = sb.Append(dash, 3);             // ---
      sb = sb.Append(tail);                // --->>>
      sb = sb.Append(tail, 2, 1);          // --->>>>

      String str = sb.ToString();
      outputBlock.Text += "The appended string is:" + "\n";
      outputBlock.Text += str + "\n";
   }
}
/*
This example produces the following results:
The appended string is:
<<<<---True, 0, 1, 2, 3, 4, 5, 6.6, 7.7, 8, 9, 10, -11--->>>>
*/

Version Information

Silverlight

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

Platforms

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

Change History

Date

History

Reason

January 2011

Expanded the Remarks section and removed the example.

Customer feedback.