System.Text.StringBuilder Class

public sealed class StringBuilder

Base Types

Object
  StringBuilder

Assembly

mscorlib

Library

BCL

Summary

Represents a mutable string of characters.

Description

This class represents string-like objects that are mutable. After a StringBuilder object has been created, it can be directly modified by removing, replacing, or inserting characters. This contrasts the String class, which represents an immutable string of characters.

The System.Text.StringBuilder.Capacity of an instance of the StringBuilder class is the maximum number of characters it can hold in the currently allocated space. The StringBuilder will dynamically allocate more space when it is required.

Unlike most types in the Base Class Library, the arguments to StringBuilder members are assumed to be passed as in / out arguments (passed by reference). [Note: Normally arguments are in arguments (passed by value) unless explicitly declared as out or in / out .]

[Note: An instance of String is said to be "immutable" because its value cannot be modified once it has been created. Methods on String that appear to modify a String instance actually return a new instance containing the modification. The StringBuilder class provides methods that actually modify the contents of a string-like object.

Relational operators only perform reference comparisons (unless overloaded by a particular language compiler). Despite this restriction, relational operators can be used to compare String objects that are assigned literal values. Their values are immutable and can't change, so a reference comparison is sufficient. Because StringBuilder instances are mutable, they should not be compared with relational operators.

For performance reasons a StringBuilder might allocate more memory than needed. The amount of memory allocated is implementation specific.

]

Attributes

DefaultMemberAttribute("Chars")

See Also

System.Text Namespace

Members

StringBuilder Constructors

StringBuilder() Constructor
StringBuilder(int) Constructor
StringBuilder(System.String) Constructor

StringBuilder Methods

StringBuilder.Append(char, int) Method
StringBuilder.Append(char[], int, int) Method
StringBuilder.Append(System.String) Method
StringBuilder.Append(System.String, int, int) Method
StringBuilder.Append(bool) Method
StringBuilder.Append(sbyte) Method
StringBuilder.Append(byte) Method
StringBuilder.Append(char) Method
StringBuilder.Append(short) Method
StringBuilder.Append(int) Method
StringBuilder.Append(long) Method
StringBuilder.Append(float) Method
StringBuilder.Append(double) Method
StringBuilder.Append(System.Decimal) Method
StringBuilder.Append(ushort) Method
StringBuilder.Append(uint) Method
StringBuilder.Append(ulong) Method
StringBuilder.Append(System.Object) Method
StringBuilder.Append(char[]) Method
StringBuilder.AppendFormat(System.String, System.Object) Method
StringBuilder.AppendFormat(System.String, System.Object, System.Object) Method
StringBuilder.AppendFormat(System.String, System.Object, System.Object, System.Object) Method
StringBuilder.AppendFormat(System.String, System.Object[]) Method
StringBuilder.AppendFormat(System.IFormatProvider, System.String, System.Object[]) Method
StringBuilder.EnsureCapacity Method
StringBuilder.Equals Method
StringBuilder.Insert(int, System.String, int) Method
StringBuilder.Insert(int, System.String) Method
StringBuilder.Insert(int, bool) Method
StringBuilder.Insert(int, sbyte) Method
StringBuilder.Insert(int, byte) Method
StringBuilder.Insert(int, short) Method
StringBuilder.Insert(int, char) Method
StringBuilder.Insert(int, char[]) Method
StringBuilder.Insert(int, char[], int, int) Method
StringBuilder.Insert(int, int) Method
StringBuilder.Insert(int, long) Method
StringBuilder.Insert(int, float) Method
StringBuilder.Insert(int, double) Method
StringBuilder.Insert(int, System.Decimal) Method
StringBuilder.Insert(int, ushort) Method
StringBuilder.Insert(int, uint) Method
StringBuilder.Insert(int, ulong) Method
StringBuilder.Insert(int, System.Object) Method
StringBuilder.Remove Method
StringBuilder.Replace(System.String, System.String) Method
StringBuilder.Replace(System.String, System.String, int, int) Method
StringBuilder.Replace(char, char) Method
StringBuilder.Replace(char, char, int, int) Method
StringBuilder.ToString() Method
StringBuilder.ToString(int, int) Method

StringBuilder Properties

StringBuilder.Capacity Property
StringBuilder.Chars Property
StringBuilder.Length Property


StringBuilder() Constructor

public StringBuilder();

Summary

Constructs and initializes a new, empty instance of the StringBuilder class.

Description

The new instance of StringBuilder represents a string equal to System.String.Empty. The System.Text.StringBuilder.Capacity is set to the default capacity.

[Note: The default value of the System.Text.StringBuilder.Capacity property is implementation dependent.]

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder(int) Constructor

public StringBuilder(int capacity);

Summary

Constructs and initializes a new, empty instance of the StringBuilder class, with a specified System.Text.StringBuilder.Capacity .

Parameters

capacity
A Int32 containing the starting number of characters allowed in the StringBuilder .

Description

If the specified capacity is less than the default capacity, the System.Text.StringBuilder.Capacity of the new instance of StringBuilder is set to the default value. The StringBuilder will dynamically allocate more space when it is required.

The new StringBuilder is initialized to represent an empty string.

[Note: The default value of the System.Text.StringBuilder.Capacity property is implementation dependent.]

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder(System.String) Constructor

public StringBuilder(string value);

Summary

Constructs and initializes a new instance of the StringBuilder class, with the specified String as its value.

Parameters

value
A String containing the string value of the new instance of StringBuilder.

Description

[Note: The System.Text.StringBuilder.Capacity of the new instance is implementation defined.]

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(char, int) Method

public StringBuilder Append(char value, int repeatCount);

Summary

Appends multiple copies of a character to the end of the current StringBuilder .

Parameters

value
The Char to be appended.
repeatCount
A Int32 containing the number of times to append value .

Return Value

The current instance after peroration has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionrepeatCount is less than zero.

Description

This method appends repeatCount copies of the specified character to the current instance.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(char[], int, int) Method

public StringBuilder Append(char[] value, int startIndex, int charCount);

Summary

Appends the string representation of an array of Unicode characters to the end of the current instance.

Parameters

value
The Char array to be appended.
startIndex
A Int32 containing the index in value at which the subarray starts.
charCount
A Int32 containing the number of characters to copy from value .

Return Value

The current instance after peroration has occurred.

Exceptions

Exception TypeCondition
ArgumentNullExceptionvalue is a null reference, and startIndex and charCount are not both zero.
ArgumentOutOfRangeExceptioncharCount or startIndex is less than zero.

-or-

The sum of startIndex and charCount is greater than the length of value.

Description

This method appends with the specified range of characters from the value array to the current instance. If value is a null reference, and both startIndex and charCount are zero, no changes are made.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(System.String) Method

public StringBuilder Append(string value);

Summary

Appends a copy of a string to the end of the current instance.

Parameters

value
The String to be appended.

Return Value

The current instance after peroration has occurred.

Description

This method appends the value string to the current instance. If value is a null reference, no changes are made.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(System.String, int, int) Method

public StringBuilder Append(string value, int startIndex, int count);

Summary

Appends a copy of an array of Unicode characters, specified by a starting index and length, of a specified String to the end of the current instance.

Parameters

value
The String from which the substring will be taken.
startIndex
A Int32 containing the index in value from which to start copying.
count
A Int32 containing the number of characters to copy from value .

Return Value

The current instance after peroration has occurred.

Exceptions

Exception TypeCondition
ArgumentNullExceptionvalue is a null reference, and startIndex and count are not both zero.
ArgumentOutOfRangeExceptioncharCount or startIndex is less than zero.

-or-

The sum of startIndex and charCount is greater than the length of value .

Description

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

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(bool) Method

public StringBuilder Append(bool value);

Summary

Appends the string representation of a Boolean to the end of the current instance.

Parameters

value
A Boolean to be appended.

Return Value

The current instance after peroration has occurred.

Description

This method appends value.ToString() to the current instance.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(sbyte) Method

public StringBuilder Append(sbyte value);

Summary

Appends the string representation of a SByte to the end of the current instance.

Parameters

value
The SByte to be appended.

Return Value

The current instance after peroration has occurred.

Description

This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Append(System.Char,System.Int32)(Int16).

This method appends value.ToString() to the current instance.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

Attributes

CLSCompliantAttribute(false)

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(byte) Method

public StringBuilder Append(byte value);

Summary

Appends the string representation of a Byte to the end of the current instance.

Parameters

value
The Byte to be appended.

Return Value

The current instance after peroration has occurred.

Description

This method appends value.ToString() to the current instance.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(char) Method

public StringBuilder Append(char value);

Summary

Appends the string representation of a Unicode character to the end of the current instance.

Parameters

value
The Char to be appended.

Return Value

The current instance after peroration has occurred.

Description

This method appends the specified character to the current instance.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(short) Method

public StringBuilder Append(short value);

Summary

Appends the string representation of a Int16 to the end of the current instance.

Parameters

value
The Int16 to be appended.

Return Value

The current instance after peroration has occurred.

Description

This method appends value.ToString() to the current instance.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(int) Method

public StringBuilder Append(int value);

Summary

Appends the string representation of a Int32 to the end of the current instance.

Parameters

value
The Int32 to be appended.

Return Value

The current instance after peroration has occurred.

Description

This method appends value.ToString() to the current instance.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(long) Method

public StringBuilder Append(long value);

Summary

Appends the string representation of a Int64 to the end of the current instance.

Parameters

value
The Int64 to be appended.

Return Value

The current instance after peroration has occurred.

Description

This method appends value.ToString() to the current instance.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(float) Method

public StringBuilder Append(float value);

Summary

Appends the string representation of a Single to the end of the current instance.

Parameters

value
The Single to be appended.

Return Value

The current instance after peroration has occurred.

Description

This method appends value.ToString() to the current instance.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

Library

ExtendedNumerics

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(double) Method

public StringBuilder Append(double value);

Summary

Appends the string representation of a Double to the end of the current instance.

Parameters

value
The Double to be appended.

Return Value

The current instance after peroration has occurred.

Description

This method appends value.ToString() to the current instance.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

Library

ExtendedNumerics

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(System.Decimal) Method

public StringBuilder Append(decimal value);

Summary

Appends the string representation of a Decimal to the end of the current instance.

Parameters

value
The Decimal to be appended.

Return Value

The current instance after peroration has occurred.

Description

This method appends value.ToString() to the current instance.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

Library

ExtendedNumerics

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(ushort) Method

public StringBuilder Append(ushort value);

Summary

Appends the string representation of a UInt16 to the end of the current instance.

Parameters

value
The UInt16 to be appended.

Return Value

The current instance after peroration has occurred.

Description

This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Append(System.Char,System.Int32)(Int32).

This method appends value.ToString() to the current instance.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

Attributes

CLSCompliantAttribute(false)

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(uint) Method

public StringBuilder Append(uint value);

Summary

Appends the string representation of a UInt32 to the end of the current instance.

Parameters

value
The UInt32 to be appended.

Return Value

The current instance after peroration has occurred.

Description

This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Append(System.Char,System.Int32)(Int64).

This method appends value.ToString() to the current instance.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

Attributes

CLSCompliantAttribute(false)

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(ulong) Method

public StringBuilder Append(ulong value);

Summary

Appends the string representation of a UInt64 to the end of the current instance.

Parameters

value
The UInt64 to be appended.

Return Value

The current instance after peroration has occurred.

Description

This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Append(System.Char,System.Int32)(Decimal).

This method appends value.ToString() to the current instance.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

Attributes

CLSCompliantAttribute(false)

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(System.Object) Method

public StringBuilder Append(object value);

Summary

Appends the string representation of an object to the end of the current instance.

Parameters

value
The Object to be appended.

Return Value

The current instance after peroration has occurred.

Description

This method appends value.ToString() to the current instance. If value is a null reference, no changes are made.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Append(char[]) Method

public StringBuilder Append(char[] value);

Summary

Appends the string representation of all of the characters in a Array to the end of the current instance.

Parameters

value
The array of Char to be appended.

Return Value

The current instance after peroration has occurred.

Description

This method appends all of the characters in the specified array to the current instance in the same order as they appear in value. If value is a null reference no changes are made.

The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.AppendFormat(System.String, System.Object) Method

public StringBuilder AppendFormat(string format, object arg0);

Summary

Appends the specified string to the current instance, with the format specifications in that string being replaced with the appropriately formatted string value of the specified object.

Parameters

format
A String containing zero or more format specifications.
arg0
A Object to be formatted.

Return Value

The current instance after peroration has occurred.

Exceptions

Exception TypeCondition
ArgumentNullExceptionformat or arg0 is a null reference.
FormatExceptionformat is invalid.

Description

This method appends the formatted copy of the specified string to the current instance.

[Note: This version of System.Text.StringBuilder.AppendFormat(System.String,System.Object) is equivalent to System.Text.StringBuilder.AppendFormat(System.String,System.Object)( null , format, new Object[] {arg0} ). For more information on the format specification, see the String class overview. ]

Example

using System;
using System.Text;

public class StringBuilderTest {
  public static void Main() { 
     
      StringBuilder sb = new StringBuilder("The high ");
      Console.WriteLine( sb.AppendFormat("temperature today was {0, 6}.", 88) ); 
  }
}
      
The output is

The high temperature today was     88.
 

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.AppendFormat(System.String, System.Object, System.Object) Method

public StringBuilder AppendFormat(string format, object arg0, object arg1);

Summary

Appends the specified string to the current instance, with the format specifications in that string being replaced with the appropriately formatted string values of the specified objects.

Parameters

format
A String containing zero or more format specifications.
arg0
The first Object to be formatted. Can be a null reference.
arg1
The second Object to be formatted. Can be a null reference.

Return Value

The current instance after peroration has occurred.

Exceptions

Exception TypeCondition
ArgumentNullExceptionformat is a null reference.
FormatExceptionformat is invalid.

Description

This method appends the formatted copy of the specified string to the current instance. If an object referenced in the format string is null , an empty string is used in its place.

[Note: This version of System.Text.StringBuilder.AppendFormat(System.String,System.Object) is equivalent to System.Text.StringBuilder.AppendFormat(System.String,System.Object)( null , format, new Object[] {arg0, arg1} ). For more information on the format specification, see the String class overview.]

Example

using System;
using System.Text;

public class StringBuilderTest {
  public static void Main() {

     StringBuilder sb = new StringBuilder("The high ");
     Console.WriteLine( sb.AppendFormat("temperature today was {0} {1}.", "very", "high") ); 
  }
}
   
The output is

The high temperature today was very high.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.AppendFormat(System.String, System.Object, System.Object, System.Object) Method

public StringBuilder AppendFormat(string format, object arg0, object arg1, object arg2);

Summary

Appends the specified string to the current instance, with the format specifications in that string being replaced with the appropriately formatted string values of the specified objects.

Parameters

format
A String containing zero or more format specifications.
arg0
The first Object to be formatted. Can be a null reference.
arg1
The second Object to be formatted. Can be a null reference.
arg2
The third Object to be formatted. Can be a null reference.

Return Value

The current instance after peroration has occurred.

Exceptions

Exception TypeCondition
ArgumentNullExceptionformat is a null reference.
FormatExceptionformat is invalid.

Description

This method appends the formatted copy of the specified string to the current instance. If an object referenced in the format string is null , an empty string is used in its place.

[Note: This version of System.Text.StringBuilder.AppendFormat(System.String,System.Object) is equivalent to System.Text.StringBuilder.AppendFormat(System.String,System.Object)( null , format, new Object[] {arg0, arg1, arg2} ). For more information on the format specification, see the String class overview. ]

Example

using System;
using System.Text;

public class StringBuilderTest {
  public static void Main() {

     StringBuilder sb = new StringBuilder("The high ");
     Console.WriteLine( sb.AppendFormat("temperature today was {0} {1} {2}.", "very", "very", "high") ); 
  }
}
   
The output is

The high temperature today was very very high.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.AppendFormat(System.String, System.Object[]) Method

public StringBuilder AppendFormat(string format, params object[] args);

Summary

Appends the specified string to the current instance, with the format specifications in that string being replaced with the appropriately formatted string values of the elements in the specified array.

Parameters

format
A String containing zero or more format specifications.
args
A Object array to be formatted.

Return Value

The current instance after peroration has occurred.

Exceptions

Exception TypeCondition
ArgumentNullExceptionformat or args is a null reference.
FormatExceptionformat is invalid.

Description

This method appends the formatted copy of the specified string to the current instance. If an object referenced in the format string is null , an empty string is used in its place.

[Note: This version of System.Text.StringBuilder.AppendFormat(System.String,System.Object) is equivalent to System.Text.StringBuilder.AppendFormat(System.String,System.Object)( null , format, args ). For more information on the format specification see the String class overview. ]

Example

using System;
using System.Text;

public class StringBuilderTest {
  public static void Main() {

     string [] strings = {"very", "very", null, "high"};

     StringBuilder sb = new StringBuilder("The high ");
     Console.WriteLine( sb.AppendFormat("temperature today was {0}, {1} {2}{3}.", strings) ); 
  }
}
   
The output is

The high temperature today was very, very high.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.AppendFormat(System.IFormatProvider, System.String, System.Object[]) Method

public StringBuilder AppendFormat(IFormatProvider provider, string format, params object[] args);

Summary

Appends the specified string to the current instance, with the format specifications in that string being replaced with the string values of the specified array of objects, formatted in accordance with the formatting object returned by the specified IFormatProvider.

Parameters

provider
A IFormatProvider that supplies a formatting object that provides culture specific formatting information. Can be a null reference.
format
A String containing zero or more format specifications.
args
A Object array to be formatted.

Return Value

The current instance after peroration has occurred.

Exceptions

Exception TypeCondition
ArgumentNullExceptionformat or args is a null reference.
FormatExceptionformat is invalid.

Description

This method appends the formatted copy of the specified string to the current instance. If an object referenced in the format string is null , an empty string is used in its place.

The format parameter is embedded with zero or more format specifications of the form, {N [, M ][: formatString ]}, where N is a zero-based integer indicating the argument to be formatted, M is an optional integer indicating the width of the region to contain the formatted value, and formatString is an optional string of formatting codes. [Note: For more information on the format specification see the String class overview.]

Example

using System;
using System.Text;

public class StringBuilderTest {
  public static void Main() {

     string a = "very";
     string b = "very";
     string c = "high";

     StringBuilder sb = new StringBuilder("The high ");
     Console.WriteLine(sb.AppendFormat(null, "temperature today was {0}, {1} {2}.", a, b, c) ); 
  }
}
   
The output is

The high temperature today was very, very high.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.EnsureCapacity Method

public int EnsureCapacity(int capacity);

Summary

Ensures that the capacity of the current instance is at least a specified value.

Parameters

capacity
A Int32 containing the minimum capacity to ensure.

Return Value

A Int32 equal to the new System.Text.StringBuilder.Capacity of the current instance.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptioncapacity is less zero.

Description

If the specified value is less than the current System.Text.StringBuilder.Capacity, no changes are made and System.Text.StringBuilder.Capacity remains the same.

[Note: For performance reasons, the new System.Text.StringBuilder.Capacity might be larger than the specified value. The amount of memory allocated by this method is implementation specific.]

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Equals Method

public bool Equals(StringBuilder sb);

Summary

Determines whether the current instance and a specified StringBuilder have the same value.

Parameters

sb
A StringBuilder.

Return Value

true if the current instance and sb have the same value; otherwise, false .

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, System.String, int) Method

public StringBuilder Insert(int index, string value, int count);

Summary

Inserts multiple copies of a string into the current instance at a specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The String to be inserted.
count
A Int32 containing the number of times the string is to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero

-or-

count is less than zero

Description

This method inserts the value string count times into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

If value is Empty or a null reference, the StringBuilder is not changed.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, System.String) Method

public StringBuilder Insert(int index, string value);

Summary

Inserts the string representation of a string object into the current instance at a specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The String to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero.

Description

This method inserts value into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

If value is Empty or a null reference, the StringBuilder is not changed.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, bool) Method

public StringBuilder Insert(int index, bool value);

Summary

Inserts the string representation of a Boolean value into the current instance at a specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The Boolean value to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero

Description

This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, sbyte) Method

public StringBuilder Insert(int index, sbyte value);

Summary

Inserts the string representation of a SByte into the current instance at the specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The SByte to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero

Description

This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Insert(System.Int32,System.String,System.Int32)(Int32, Int16).

This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

Attributes

CLSCompliantAttribute(false)

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, byte) Method

public StringBuilder Insert(int index, byte value);

Summary

Inserts the string representation of a Byte into the current instance at the specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The Byte to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero

Description

This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, short) Method

public StringBuilder Insert(int index, short value);

Summary

Inserts the string representation of a Int16 into the current instance at a specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The Int16 to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero.

Description

This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, char) Method

public StringBuilder Insert(int index, char value);

Summary

Inserts the string representation of a Unicode character into the current instance at a specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The Char to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero.

Description

This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, char[]) Method

public StringBuilder Insert(int index, char[] value);

Summary

Inserts the string representation of an array of Unicode characters into the current instance at a specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The Char array to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero.

Description

This method inserts all of the characters in the specified array into the current instance in the same order as they appear in value. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

If value is empty or a null reference, the StringBuilder is not changed.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, char[], int, int) Method

public StringBuilder Insert(int index, char[] value, int startIndex, int charCount);

Summary

Inserts the string representation of a subarray of Unicode characters into the current instance at a specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The Char array from which to get the characters to be inserted.
startIndex
A Int32 containing the starting index within value.
charCount
A Int32 containing the number of characters to insert from value.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentNullExceptionvalue is a null reference, and startIndex and charCount are not both zero.
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance or less than zero.

-or-

startIndex or charCount is less than zero or their sum is greater than the length of value.

Description

This method inserts the specified range of characters from value array into the current instance in the same order as they appear in value. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

If value is an empty array or a null reference and startIndex and charCount are both zero, the StringBuilder is not changed.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, int) Method

public StringBuilder Insert(int index, int value);

Summary

Inserts the string representation of a Int32 into the current instance at the specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The Int32 to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero

Description

This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, long) Method

public StringBuilder Insert(int index, long value);

Summary

Inserts the string representation of a Int64 into the current instance at the specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The Int64 to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero.

Description

This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, float) Method

public StringBuilder Insert(int index, float value);

Summary

Inserts the string representation of a Single into the current instance at the specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The Single to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero

Description

This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

Library

ExtendedNumerics

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, double) Method

public StringBuilder Insert(int index, double value);

Summary

Inserts the string representation of a Double into the current instance at the specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The Double to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero

Description

This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

Library

ExtendedNumerics

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, System.Decimal) Method

public StringBuilder Insert(int index, decimal value);

Summary

Inserts the string representation of a Decimal into the current instance at the specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The Decimal to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero

Description

This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

Library

ExtendedNumerics

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, ushort) Method

public StringBuilder Insert(int index, ushort value);

Summary

Inserts the string representation of a UInt16 into the current instance at the specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The UInt16 to insert.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero.

Description

This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Insert(System.Int32,System.String,System.Int32)(Int32, Int32).

This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

Attributes

CLSCompliantAttribute(false)

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, uint) Method

public StringBuilder Insert(int index, uint value);

Summary

Inserts the string representation of a UInt32 into the current instance at the specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The UInt32 to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero.

Description

This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Insert(System.Int32,System.String,System.Int32)(Int32, Int64).

This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

Attributes

CLSCompliantAttribute(false)

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, ulong) Method

public StringBuilder Insert(int index, ulong value);

Summary

Inserts the string representation of a UInt64 into the current instance at a specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The UInt64 to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero.

Description

This member is not CLS-compliant. For a CLS-compliant alternative, use System.Text.StringBuilder.Insert(System.Int32,System.String,System.Int32)(Int32, Decimal).

This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

Attributes

CLSCompliantAttribute(false)

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Insert(int, System.Object) Method

public StringBuilder Insert(int index, object value);

Summary

Inserts the string representation of an object into the current instance at a specified index.

Parameters

index
A Int32 containing the index at which to insert.
value
The Object to be inserted.

Return Value

The current instance after insertion has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionindex is greater than the length of the current instance

-or-

index is less than zero.

Description

This method inserts value.ToString() into the current instance at the specified location. Existing characters are shifted to make room for the new text, and System.Text.StringBuilder.Capacity is adjusted as necessary.

If value is a null reference, the StringBuilder is not changed.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Remove Method

public StringBuilder Remove(int startIndex, int length);

Summary

Removes a specified range of characters from the current instance.

Parameters

startIndex
A Int32 containing the index at which to begin removal.
length
A Int32 containing the number of characters to be removed.

Return Value

The current instance after removal has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionstartIndex or length is less than zero

-or-

The sum of startIndex and length is greater than the length of the current instance.

Description

This method removes the specified range of characters from the current instance. The characters at (startIndex + length) are moved to startIndex, and the string value of the current instance is shortened by length.

[Note: The System.Text.StringBuilder.Replace(System.String,System.String) method can be used to remove all instances of a string from a StringBuilder. ]

Example

using System;
using System.Text;

public class StringBuilderTest  {
   public static void Main()  {
   
      StringBuilder sb = new StringBuilder("0123456789");
      Console.WriteLine(sb);
      sb.Remove(3, 4);
      Console.WriteLine(sb);
   }
}
   
The output is

0123456789

012789

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Replace(System.String, System.String) Method

public StringBuilder Replace(string oldValue, string newValue);

Summary

Replaces all instances of a specified string with another specified string.

Parameters

oldValue
A String containing the string value to replace.
newValue
A String containing the string value with which to replace oldValue. Can be a null reference.

Return Value

The current instance after substitution has occurred.

Exceptions

Exception TypeCondition
ArgumentNullExceptionoldValue is a null reference.
ArgumentExceptionThe length of oldvalue is zero.

Description

This method substitutes each occurrence of oldValue in the current instance with newValue. [Note: If newValue is null , instances of oldValue are removed.]

This method is case-sensitive.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Replace(System.String, System.String, int, int) Method

public StringBuilder Replace(string oldValue, string newValue, int startIndex, int count);

Summary

Replaces all instances of a specified string in a specified range with another specified string.

Parameters

oldValue
A String containing the string value to replace.
newValue
A String containing the string value with which to replace oldValue. Can be a null reference.
startIndex
A Int32 containing the location from which to start replacing oldValue.
count
A Int32 containing the length of the range in which to replace oldValue.

Return Value

The current instance after substitution has occurred.

Exceptions

Exception TypeCondition
ArgumentNullExceptionoldValue is a null reference.

ArgumentOutOfRangeExceptionstartIndex or count is less than zero.

-or-

The sum of startIndex and count is greater than the length of the current instance.

ArgumentExceptionThe length of oldvalue is zero.

Description

This method substitutes each occurrence of oldValue in the specified range of the current instance with newValue. [Note: If newValue is null , instances of oldValue are removed.]

This method is case-sensitive.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Replace(char, char) Method

public StringBuilder Replace(char oldChar, char newChar);

Summary

Replaces all instances of a specified character in the current instance with another specified character.

Parameters

oldChar
The Char to replace.
newChar
The Char with which to replace oldChar .

Return Value

The current instance after substitution has occurred.

Description

This method substitutes each occurrence of oldChar in the current instance with newChar.

This method is case-sensitive.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Replace(char, char, int, int) Method

public StringBuilder Replace(char oldChar, char newChar, int startIndex, int count);

Summary

Replaces all instances of a specified character in a specified range with another specified character.

Parameters

oldChar
The Char to replace.
newChar
The Char with which to replace oldChar .
startIndex
A Int32 containing the index from which to start replacing oldChar.
count
A Int32 containing the length of the range in which to replace oldChar .

Return Value

The current instance after substitution has occurred.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe sum of startIndex and count is larger than the length of the current instance

-or-

startIndex or count is less than zero.

Description

This method substitutes each occurrence of oldChar in the specified range of the current instance with newChar.

This method is case-sensitive.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.ToString() Method

public override string ToString();

Summary

Returns a String representation of the value of the current instance.

Return Value

A String representing the current instance.

Description

This method overrides System.Object.ToString.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.ToString(int, int) Method

public string ToString(int startIndex, int length);

Summary

Returns a String representation of a substring of the current instance.

Parameters

startIndex
A Int32 containing the index at which the substring begins.
length
A Int32 containing the length of the substring.

Return Value

A new String representing the characters in the specified range.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionstartIndex or length is less than zero.

-or-

The sum of startIndex and length is greater than the length of the current instance.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Capacity Property

public int Capacity { get; set; }

Summary

Gets or sets the maximum number of characters that can be contained in the memory allocated by the current instance.

Property Value

A Int32 containing the maximum number of characters that can be contained in the memory allocated by the current instance.

Exceptions

Exception TypeCondition
ArgumentExceptionThe value specified for a set operation is less than System.Text.StringBuilder.Length.

Description

The System.Text.StringBuilder.Capacity property does not affect the string value of the current instance. The StringBuilder will dynamically increase the System.Text.StringBuilder.Capacity and allocate more space when it is required.

[Note: For performance reasons a StringBuilder might allocate more memory than needed. The amount of memory allocated is implementation specific. ]

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Chars Property

public char this[int index] { get; set; }

Summary

Gets or sets the character at a specified position in the current instance.

Property Value

A Char containing the Unicode character at location index in the current instance.

Exceptions

Exception TypeCondition
IndexOutOfRangeExceptionindex is greater than or equal to the length of the current instance.

-or-

index is less than zero.

Description

index is the position of a character within the StringBuilder. The first character in the string is at index 0. The length of a string is the number of characters it contains. The last accessible character of a StringBuilder instance is at the index System.Text.StringBuilder.Length - 1.

See Also

System.Text.StringBuilder Class, System.Text Namespace

StringBuilder.Length Property

public int Length { get; set; }

Summary

Gets or sets the length of the current instance.

Property Value

A Int32 containing the length of the current instance.

Exceptions

Exception TypeCondition
ArgumentOutOfRangeExceptionThe value specified for a set operation is less than 0 or greater than System.Int32.MaxValue.

Description

If the specified length is less than the current length, the StringBuilder is truncated to the specified length. If the specified length is greater than the current length, the end of the string value of the StringBuilder is padded with spaces.

If the specified length is greater than the current System.Text.StringBuilder.Capacity , System.Text.StringBuilder.Capacity is set to the specified length.

[Note: A space in Unicode format is defined as the hexadecimal value 0x20.]

See Also

System.Text.StringBuilder Class, System.Text Namespace