public sealed class StringBuilder
Object
StringBuilder
mscorlib
BCL
Represents a mutable string of characters.
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 arein
arguments (passed by value) unless explicitly declared asout
orin
/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.
]
DefaultMemberAttribute("Chars")
System.Text Namespace
StringBuilder Constructors
StringBuilder() Constructor
StringBuilder(System.String) Constructor
StringBuilder(int) 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, System.Object, System.Object) Method
StringBuilder.AppendFormat(System.IFormatProvider, System.String, System.Object[]) Method
StringBuilder.AppendFormat(System.String, System.Object[]) Method
StringBuilder.AppendFormat(System.String, System.Object, System.Object) Method
StringBuilder.AppendFormat(System.String, System.Object) Method
StringBuilder.EnsureCapacity Method
StringBuilder.Equals Method
StringBuilder.Insert(int, System.String) Method
StringBuilder.Insert(int, bool) Method
StringBuilder.Insert(int, System.String, int) 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, System.Object) Method
StringBuilder.Insert(int, ulong) Method
StringBuilder.Insert(int, uint) Method
StringBuilder.Insert(int, ushort) Method
StringBuilder.Insert(int, System.Decimal) Method
StringBuilder.Insert(int, double) Method
StringBuilder.Insert(int, float) Method
StringBuilder.Insert(int, long) Method
StringBuilder.Insert(int, int) Method
StringBuilder.Remove Method
StringBuilder.Replace(char, char, int, int) Method
StringBuilder.Replace(char, char) Method
StringBuilder.Replace(System.String, System.String, int, int) Method
StringBuilder.Replace(System.String, System.String) Method
StringBuilder.ToString(int, int) Method
StringBuilder.ToString() Method
StringBuilder Properties
StringBuilder.Capacity Property
StringBuilder.Chars Property
StringBuilder.Length Property
public StringBuilder();
Constructs and initializes a new, empty instance of the StringBuilder class.
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.]
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder(string value);
Constructs and initializes a new instance of the StringBuilder class, with the specified String as its value.
- value
- A String containing the string value of the new instance of StringBuilder.
[Note: The System.Text.StringBuilder.Capacity of the new instance is implementation defined.]
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder(int capacity);
Constructs and initializes a new, empty instance of the StringBuilder class, with a specified System.Text.StringBuilder.Capacity .
- capacity
- A Int32 containing the starting number of characters allowed in the StringBuilder .
Exception Type Condition ArgumentOutOfRangeException capacity is less than zero.
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.]
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(char value, int repeatCount);
Appends multiple copies of a character to the end of the current StringBuilder .
- value
- The Char to be appended.
- repeatCount
- A Int32 containing the number of times to append value .
The current instance after the operation has occurred.
Exception Type Condition ArgumentOutOfRangeException repeatCount is less than zero.
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(char[] value, int startIndex, int charCount);
Appends the string representation of an array of Unicode characters to the end of the current instance.
- 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 .
The current instance after the operation has occurred.
Exception Type Condition ArgumentNullException value is a null reference, and startIndex and charCount are not both zero. ArgumentOutOfRangeException charCount or startIndex is less than zero. -or-
The sum of startIndex and charCount is greater than the length of value.
This method appends 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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(string value);
Appends a copy of a string to the end of the current instance.
- value
- The String to be appended.
The current instance after the operation has occurred.
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(string value, int startIndex, int count);
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.
- 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 .
The current instance after the operation has occurred.
Exception Type Condition ArgumentNullException value is a null reference, and startIndex and count are not both zero. ArgumentOutOfRangeException charCount or startIndex is less than zero. -or-
The sum of startIndex and charCount is greater than the length of value .
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(bool value);
Appends the string representation of a Boolean to the end of the current instance.
- value
- A Boolean to be appended.
The current instance after the operation has occurred.
This method appends value.ToString() to the current instance.The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(sbyte value);
Appends the string representation of a SByte to the end of the current instance.
- value
- The SByte to be appended.
The current instance after the operation has occurred.
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.
CLSCompliantAttribute(false)
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(byte value);
Appends the string representation of a Byte to the end of the current instance.
- value
- The Byte to be appended.
The current instance after the operation has occurred.
This method appends value.ToString() to the current instance.The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(char value);
Appends the string representation of a Unicode character to the end of the current instance.
- value
- The Char to be appended.
The current instance after the operation has occurred.
This method appends the specified character to the current instance.The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(short value);
Appends the string representation of a Int16 to the end of the current instance.
- value
- The Int16 to be appended.
The current instance after the operation has occurred.
This method appends value.ToString() to the current instance.The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(int value);
Appends the string representation of a Int32 to the end of the current instance.
- value
- The Int32 to be appended.
The current instance after the operation has occurred.
This method appends value.ToString() to the current instance.The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(long value);
Appends the string representation of a Int64 to the end of the current instance.
- value
- The Int64 to be appended.
The current instance after the operation has occurred.
This method appends value.ToString() to the current instance.The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(float value);
Appends the string representation of a Single to the end of the current instance.
- value
- The Single to be appended.
The current instance after the operation has occurred.
This method appends value.ToString() to the current instance.The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.
ExtendedNumerics
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(double value);
Appends the string representation of a Double to the end of the current instance.
- value
- The Double to be appended.
The current instance after the operation has occurred.
This method appends value.ToString() to the current instance.The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.
ExtendedNumerics
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(decimal value);
Appends the string representation of a Decimal to the end of the current instance.
- value
- The Decimal to be appended.
The current instance after the operation has occurred.
This method appends value.ToString() to the current instance.The System.Text.StringBuilder.Capacity of the current instance is increased as necessary.
ExtendedNumerics
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(ushort value);
Appends the string representation of a UInt16 to the end of the current instance.
- value
- The UInt16 to be appended.
The current instance after the operation has occurred.
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.
CLSCompliantAttribute(false)
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(uint value);
Appends the string representation of a UInt32 to the end of the current instance.
- value
- The UInt32 to be appended.
The current instance after the operation has occurred.
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.
CLSCompliantAttribute(false)
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(ulong value);
Appends the string representation of a UInt64 to the end of the current instance.
- value
- The UInt64 to be appended.
The current instance after the operation has occurred.
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.
CLSCompliantAttribute(false)
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(object value);
Appends the string representation of an object to the end of the current instance.
- value
- The Object to be appended.
The current instance after the operation has occurred.
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Append(char[] value);
Appends the string representation of all of the characters in a Array to the end of the current instance.
- value
- The array of Char to be appended.
The current instance after the operation has occurred.
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder AppendFormat(string format, object arg0, object arg1, object arg2);
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.
- 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.
The current instance after the operation has occurred.
Exception Type Condition ArgumentNullException format is a null reference. FormatException format is invalid.
This method appends the formatted copy of the specified string to the current instance. If an object referenced in the format string isnull
, 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. ]
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder AppendFormat(IFormatProvider provider, string format, params object[] args);
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.
- 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.
The current instance after the operation has occurred.
Exception Type Condition ArgumentNullException format or args is a null reference. FormatException format is invalid.
This method appends the formatted copy of the specified string to the current instance. If an object referenced in the format string isnull
, 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.]
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder AppendFormat(string format, params object[] args);
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.
- format
- A String containing zero or more format specifications.
- args
- A Object array to be formatted.
The current instance after operation has occurred.
Exception Type Condition ArgumentNullException format is a null reference. FormatException format is invalid.
This method appends the formatted copy of the specified string to the current instance. If an object referenced in the format string isnull
, 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. ]
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder AppendFormat(string format, object arg0, object arg1);
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.
- 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.
The current instance after the operation has occurred.
Exception Type Condition ArgumentNullException format is a null reference. FormatException format is invalid.
This method appends the formatted copy of the specified string to the current instance. If an object referenced in the format string isnull
, 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.]
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder AppendFormat(string format, object arg0);
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.
- format
- A String containing zero or more format specifications.
- arg0
- A Object to be formatted.
The current instance after the operation has occurred.
Exception Type Condition ArgumentNullException format is a null reference. FormatException format is invalid.
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. ]
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.
System.Text.StringBuilder Class, System.Text Namespace
public int EnsureCapacity(int capacity);
Ensures that the capacity of the current instance is at least a specified value.
- capacity
- A Int32 containing the minimum capacity to ensure.
A Int32 equal to the new System.Text.StringBuilder.Capacity of the current instance.
Exception Type Condition ArgumentOutOfRangeException capacity is less zero.
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.]
System.Text.StringBuilder Class, System.Text Namespace
public bool Equals(StringBuilder sb);
Determines whether the current instance and a specified StringBuilder have the same value.
- sb
- A StringBuilder.
true
if the current instance and sb have the same value; otherwise,false
.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, string value);
Inserts the string representation of a string object into the current instance at a specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The String to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero.
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 System.String.Empty or a null reference, the StringBuilder is not changed.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, bool value);
Inserts the string representation of a Boolean value into the current instance at a specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The Boolean value to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, string value, int count);
Inserts multiple copies of a string into the current instance at a specified index.
- 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.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero
-or-
count is less than zero
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, sbyte value);
Inserts the string representation of a SByte into the current instance at the specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The SByte to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero
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.
CLSCompliantAttribute(false)
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, byte value);
Inserts the string representation of a Byte into the current instance at the specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The Byte to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, short value);
Inserts the string representation of a Int16 into the current instance at a specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The Int16 to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero.
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, char value);
Inserts the string representation of a Unicode character into the current instance at a specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The Char to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero.
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, char[] value);
Inserts the string representation of an array of Unicode characters into the current instance at a specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The Char array to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero.
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, char[] value, int startIndex, int charCount);
Inserts the string representation of a subarray of Unicode characters into the current instance at a specified index.
- 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.
The current instance after insertion has occurred.
Exception Type Condition ArgumentNullException value is a null reference, and startIndex and charCount are not both zero. ArgumentOutOfRangeException index 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.
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, object value);
Inserts the string representation of an object into the current instance at a specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The Object to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero.
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, ulong value);
Inserts the string representation of a UInt64 into the current instance at a specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The UInt64 to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero.
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.
CLSCompliantAttribute(false)
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, uint value);
Inserts the string representation of a UInt32 into the current instance at the specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The UInt32 to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero.
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.
CLSCompliantAttribute(false)
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, ushort value);
Inserts the string representation of a UInt16 into the current instance at the specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The UInt16 to insert.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero.
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.
CLSCompliantAttribute(false)
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, decimal value);
Inserts the string representation of a Decimal into the current instance at the specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The Decimal to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero
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.
ExtendedNumerics
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, double value);
Inserts the string representation of a Double into the current instance at the specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The Double to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero
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.
ExtendedNumerics
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, float value);
Inserts the string representation of a Single into the current instance at the specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The Single to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero
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.
ExtendedNumerics
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, long value);
Inserts the string representation of a Int64 into the current instance at the specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The Int64 to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero.
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Insert(int index, int value);
Inserts the string representation of a Int32 into the current instance at the specified index.
- index
- A Int32 containing the index at which to insert.
- value
- The Int32 to be inserted.
The current instance after insertion has occurred.
Exception Type Condition ArgumentOutOfRangeException index is greater than the length of the current instance -or-
index is less than zero
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.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Remove(int startIndex, int length);
Removes a specified range of characters from the current instance.
- startIndex
- A Int32 containing the index at which to begin removal.
- length
- A Int32 containing the number of characters to be removed.
The current instance after removal has occurred.
Exception Type Condition ArgumentOutOfRangeException startIndex or length is less than zero -or-
The sum of startIndex and length is greater than the length of the current instance.
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. ]
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
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Replace(char oldChar, char newChar, int startIndex, int count);
Replaces all instances of a specified character in a specified range with another specified character.
- 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 .
The current instance after substitution has occurred.
Exception Type Condition ArgumentOutOfRangeException The sum of startIndex and count is larger than the length of the current instance -or-
startIndex or count is less than zero.
This method substitutes each occurrence of oldChar in the specified range of the current instance with newChar.This method is case-sensitive.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Replace(char oldChar, char newChar);
Replaces all instances of a specified character in the current instance with another specified character.
- oldChar
- The Char to replace.
- newChar
- The Char with which to replace oldChar .
The current instance after substitution has occurred.
This method substitutes each occurrence of oldChar in the current instance with newChar.This method is case-sensitive.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Replace(string oldValue, string newValue, int startIndex, int count);
Replaces all instances of a specified string in a specified range with another specified string.
- 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.
The current instance after substitution has occurred.
Exception Type Condition ArgumentNullException oldValue is a null reference.
ArgumentOutOfRangeException startIndex or count is less than zero. -or-
The sum of startIndex and count is greater than the length of the current instance.
ArgumentException The length of oldvalue is zero.
This method substitutes each occurrence of oldValue in the specified range of the current instance with newValue. [Note: If newValue isnull
, instances of oldValue are removed.]
This method is case-sensitive.
System.Text.StringBuilder Class, System.Text Namespace
public StringBuilder Replace(string oldValue, string newValue);
Replaces all instances of a specified string with
another specified string.
- 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.
The current instance after substitution has occurred.
Exception Type Condition ArgumentNullException oldValue is a null reference. ArgumentException The length of oldvalue is zero.
This method substitutes each occurrence of oldValue in the current instance with newValue. [Note: If newValue isnull
, instances of oldValue are removed.]
This method is case-sensitive.
System.Text.StringBuilder Class, System.Text Namespace
public string ToString(int startIndex, int length);
Returns a String representation of a substring of the current instance.
- startIndex
- A Int32 containing the index at which the substring begins.
- length
- A Int32 containing the length of the substring.
A new String representing the characters in the specified range.
Exception Type Condition ArgumentOutOfRangeException startIndex or length is less than zero. -or-
The sum of startIndex and length is greater than the length of the current instance.
System.Text.StringBuilder Class, System.Text Namespace
public override string ToString();
Returns a String representation of the value of the current instance.
A String representing the current instance.
This method overrides System.Object.ToString.
System.Text.StringBuilder Class, System.Text Namespace
public int Capacity { get; set; }
Gets or sets the maximum number of characters that can be contained in the memory allocated by the current instance.
A Int32 containing the maximum number of characters that can be contained in the memory allocated by the current instance.
Exception Type Condition ArgumentException The value specified for a set operation is less than System.Text.StringBuilder.Length.
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. ]
System.Text.StringBuilder Class, System.Text Namespace
public char this[int index] { get; set; }
Gets or sets the character at a specified position in the current instance.
A Char containing the Unicode character at location index in the current instance.
Exception Type Condition IndexOutOfRangeException index is greater than or equal to the length of the current instance. -or-
index is less than zero.
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.
System.Text.StringBuilder Class, System.Text Namespace
public int Length { get; set; }
Gets or sets the length of the current instance.
A Int32 containing the length of the current instance.
Exception Type Condition ArgumentOutOfRangeException The value specified for a set operation is less than 0.
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.]
System.Text.StringBuilder Class, System.Text Namespace