Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
MSDN Library

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 StringBuilder Append(
	char[] value,
	int startIndex,
	int charCount
)

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 null, 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 null 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.


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.

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


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--->>>>
*/


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

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

Date

History

Reason

January 2011

Expanded the Remarks section and removed the example.

Customer feedback.

Community Additions

Show:
© 2015 Microsoft