Class StringBuilder
Represents a mutable string of characters. This class cannot be inherited.
public sealed class StringBuilder
- Inheritance
-
StringBuilder
- Inherited Members
- Extension Methods
Remarks
This is a port of the full .NET Framework StringBuilder class.
Contributed to the .NETMF code base by Julius Friedman.
Constructors
StringBuilder()
Initializes a new instance of the StringBuilder class.
public StringBuilder()
StringBuilder(int)
Initializes a new instance of the StringBuilder class using the specified capacity.
public StringBuilder(int capacity)
Parameters
capacityintThe suggested starting size of this instance.
StringBuilder(int, int)
Initializes a new instance of the StringBuilder class that starts with a specified capacity and can grow to a specified maximum.
public StringBuilder(int capacity, int maxCapacity)
Parameters
capacityintThe suggested starting size of the StringBuilder.
maxCapacityintThe maximum number of characters the current string can contain.
Exceptions
- ArgumentOutOfRangeException
If
capacityis less than zero,maxCapacityis less than one, orcapacityis greater thanmaxCapacity.
StringBuilder(string)
Initializes a new instance of the StringBuilder class using the specified string.
public StringBuilder(string value)
Parameters
valuestringThe string used to initialize the value of the instance. If value is null, the new StringBuilder will contain the empty string (that is, it contains Empty).
StringBuilder(string, int)
Initializes a new instance of the StringBuilder class using the specified string and capacity.
public StringBuilder(string value, int capacity)
Parameters
valuestringThe string used to initialize the value of the instance. If value is null, the new StringBuilder will contain the empty string (that is, it contains Empty).
capacityintThe suggested starting size of the StringBuilder.
StringBuilder(string, int, int, int)
Initializes a new instance of the StringBuilder class from the specified substring and capacity.
public StringBuilder(string value, int startIndex, int length, int capacity)
Parameters
valuestringThe string that contains the substring used to initialize the value of this instance. If value is null, the new StringBuilder will contain the empty string (that is, it contains Empty).
startIndexintThe position within value where the substring begins.
lengthintThe number of characters in the substring.
capacityintThe suggested starting size of the StringBuilder.
Exceptions
- ArgumentOutOfRangeException
If
capacityis less than zero,lengthis less than zero,startIndexis less than zero, orstartIndexis greater than the length ofvalueminuslength.
Properties
Capacity
Gets or sets the maximum number of characters that can be contained in the memory allocated by the current instance.
public int Capacity { get; set; }
Property Value
- int
The maximum number of characters that can be contained in the memory allocated by the current instance. Its value can range from Length to MaxCapacity.
Exceptions
- ArgumentOutOfRangeException
The value specified for a set operation is less than the current length of this instance.
-or-
The value specified for a set operation is greater than the maximum capacity.
this[int]
Gets or sets the character at the specified character position in this instance.
public char this[int index] { get; set; }
Parameters
indexintThe position of the character.
Property Value
- char
The Unicode character at position index.
Exceptions
- IndexOutOfRangeException
indexis outside the bounds of this instance while getting a character.- ArgumentOutOfRangeException
indexis outside the bounds of this instance while setting a character.
Length
Gets or sets the length of the current StringBuilder object.
public int Length { get; set; }
Property Value
- int
The length of this instance.
Exceptions
- ArgumentOutOfRangeException
If Length is set to a value that is less than zero or greater than MaxCapacity.
MaxCapacity
Gets the maximum capacity of this instance.
public int MaxCapacity { get; }
Property Value
- int
The maximum number of characters this instance can hold.
Methods
Append(bool)
Appends the string representation of a specified bool value to this instance.
public StringBuilder Append(bool value)
Parameters
valueboolThe Boolean value to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(byte)
Appends the string representation of a specified 8-bit unsigned integer to this instance.
public StringBuilder Append(byte value)
Parameters
valuebyteThe value to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(char)
Appends the string representation of a specified Unicode character to this instance.
public StringBuilder Append(char value)
Parameters
valuecharThe UTF-16-encoded code unit to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(char, int)
Appends a specified number of copies of the string representation of a Unicode character to this instance.
public StringBuilder Append(char value, int repeatCount)
Parameters
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(char[])
Appends the string representation of the Unicode characters in a specified array to this instance.
public StringBuilder Append(char[] value)
Parameters
valuechar[]The array of characters to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Remarks
This method appends all the characters in the specified array to the current instance in the same order as they appear in value. If value is null, no changes are made.
Append(char[], int, int)
Appends the string representation of a specified subarray of Unicode characters to this instance
public StringBuilder Append(char[] value, int startIndex, int charCount)
Parameters
valuechar[]A character array.
startIndexintThe starting position in value.
charCountintThe number of characters to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(double)
Appends the string representation of a specified double-precision floating-point number to this instance.
public StringBuilder Append(double value)
Parameters
valuedoubleThe value to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(short)
Appends the string representation of a specified 16-bit signed integer to this instance.
public StringBuilder Append(short value)
Parameters
valueshortThe value to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(int)
Appends the string representation of a specified 32-bit signed integer to this instance.
public StringBuilder Append(int value)
Parameters
valueintThe value to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(long)
Appends the string representation of a specified 64-bit unsigned integer to this instance.
public StringBuilder Append(long value)
Parameters
valuelongThe value to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(object)
Appends the string representation of a specified object to this instance.
public StringBuilder Append(object value)
Parameters
valueobjectThe object to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(sbyte)
Appends the string representation of a specified 8-bit signed integer to this instance.
public StringBuilder Append(sbyte value)
Parameters
valuesbyteThe value to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(float)
Appends the string representation of a specified double-precision floating-point number to this instance.
public StringBuilder Append(float value)
Parameters
valuefloatThe value to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(string)
Appends a copy of the specified string to this instance.
public StringBuilder Append(string value)
Parameters
valuestringThe string to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(string, int, int)
Appends a copy of a specified substring to this instance.
public StringBuilder Append(string value, int startIndex, int count)
Parameters
valuestringThe string that contains the substring to append.
startIndexintThe starting position of the substring within value.
countintThe number of characters in value to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(ushort)
Appends the string representation of a specified 16-bit unsigned integer to this instance.
public StringBuilder Append(ushort value)
Parameters
valueushortThe value to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(uint)
Appends the string representation of a specified 32-bit unsigned integer to this instance.
public StringBuilder Append(uint value)
Parameters
valueuintThe value to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
Append(ulong)
Appends the string representation of a specified 64-bit unsigned integer to this instance.
public StringBuilder Append(ulong value)
Parameters
valueulongThe value to append.
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
AppendLine()
Appends the default line terminator to the end of the current StringBuilder object.
public StringBuilder AppendLine()
Returns
- StringBuilder
A reference to this instance after the append operation has completed.
AppendLine(string)
Appends a copy of the specified string followed by the default line terminator to the end of the current StringBuilder object.
public StringBuilder AppendLine(string str)
Parameters
strstringA reference to this instance after the append operation has completed.
Returns
Clear()
Removes all characters from the current StringBuilder instance.
public StringBuilder Clear()
Returns
- StringBuilder
An object whose Length is 0 (zero).
Insert(int, char[], int, int)
Inserts the string representation of a specified subarray of Unicode characters into this instance at the specified character position.
public StringBuilder Insert(int index, char[] value, int startIndex, int charCount)
Parameters
indexintThe position in this instance where insertion begins.
valuechar[]A character array.
startIndexintThe starting index within value.
charCountintThe number of characters to insert.
Returns
- StringBuilder
A reference to this instance after the insert operation has completed.
Insert(int, string, int)
Inserts one or more copies of a specified string into this instance at the specified character position.
public StringBuilder Insert(int index, string value, int count)
Parameters
indexintThe position in this instance where insertion begins.
valuestringThe string to insert.
countintThe number of times to insert value.
Returns
- StringBuilder
A reference to this instance after insertion has completed.
Remove(int, int)
Removes the specified range of characters from this instance.
public StringBuilder Remove(int startIndex, int length)
Parameters
startIndexintThe zero-based position in this instance where removal begins.
lengthintThe number of characters to remove.
Returns
- StringBuilder
A reference to this instance after the excise operation has completed.
Replace(char, char)
Replaces all occurrences of a specified character in this instance with another specified character.
public StringBuilder Replace(char oldChar, char newChar)
Parameters
Returns
- StringBuilder
A reference to this instance with
oldCharreplaced bynewChar.
Replace(char, char, int, int)
Replaces, within a substring of this instance, all occurrences of a specified character with another specified character.
public StringBuilder Replace(char oldChar, char newChar, int startIndex, int count)
Parameters
oldCharcharThe character to replace.
newCharcharThe character that replaces
oldChar.startIndexintThe position in this instance where the substring begins.
countintThe length of the substring.
Returns
- StringBuilder
A reference to this instance with
oldCharreplaced bynewCharin the range fromstartIndextostartIndex+count- 1.
Replace(string, string)
Replaces all occurrences of a specified string in this instance with another specified string.
public StringBuilder Replace(string oldValue, string newValue)
Parameters
Returns
- StringBuilder
A reference to this instance with all instances of
oldValuereplaced bynewValue.
Replace(string, string, int, int)
Replaces, within a substring of this instance, all occurrences of a specified string with another specified string.
public StringBuilder Replace(string oldValue, string newValue, int startIndex, int count)
Parameters
oldValuestringThe string to replace.
newValuestringThe string that replaces
oldValue, or null.startIndexintThe position in this instance where the substring begins.
countintThe length of the substring.
Returns
- StringBuilder
A reference to this instance with all instances of
oldValuereplaced bynewValuein the range fromstartIndextostartIndex+count- 1.
ToString()
Converts the value of this instance to a String. (Overrides ToString()().)
public override string ToString()
Returns
- string
A string whose value is the same as this instance.
ToString(int, int)
Converts the value of a substring of this instance to a string.
public string ToString(int startIndex, int length)
Parameters
startIndexintThe starting position of the substring in this instance.
lengthintThe length of the substring.
Returns
- string
A string whose value is the same as the specified substring of this instance.