Επεξεργασία

Κοινή χρήση μέσω


StringBuilder Constructors

Definition

Initializes a new instance of the StringBuilder class.

Overloads

StringBuilder()

Initializes a new instance of the StringBuilder class.

StringBuilder(Int32)

Initializes a new instance of the StringBuilder class using the specified capacity.

StringBuilder(String)

Initializes a new instance of the StringBuilder class using the specified string.

StringBuilder(Int32, Int32)

Initializes a new instance of the StringBuilder class that starts with a specified capacity and can grow to a specified maximum.

StringBuilder(String, Int32)

Initializes a new instance of the StringBuilder class using the specified string and capacity.

StringBuilder(String, Int32, Int32, Int32)

Initializes a new instance of the StringBuilder class from the specified substring and capacity.

StringBuilder()

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Initializes a new instance of the StringBuilder class.

public:
 StringBuilder();
public StringBuilder ();
Public Sub New ()

Examples

The following example demonstrates how to call the StringBuilder constructor with no parameters.

StringBuilder^ stringBuilder = gcnew StringBuilder;
StringBuilder stringBuilder = new StringBuilder();
let stringBuilder = StringBuilder()
Dim stringBuilder As New StringBuilder

Remarks

The string value of this instance is set to String.Empty, and the capacity is set to the implementation-specific default capacity.

Applies to

StringBuilder(Int32)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Initializes a new instance of the StringBuilder class using the specified capacity.

public:
 StringBuilder(int capacity);
public StringBuilder (int capacity);
new System.Text.StringBuilder : int -> System.Text.StringBuilder
Public Sub New (capacity As Integer)

Parameters

capacity
Int32

The suggested starting size of this instance.

Exceptions

capacity is less than zero.

Examples

The following example demonstrates how to call the StringBuilder constructor with a specified capacity.

int capacity = 255;
StringBuilder^ stringBuilder = gcnew StringBuilder( capacity );
int capacity = 255;
StringBuilder stringBuilder = new StringBuilder(capacity);
let capacity = 255
let stringBuilder = StringBuilder capacity
Dim capacity As Integer = 255
Dim stringBuilder As New StringBuilder(capacity)

Remarks

The capacity parameter defines the maximum number of characters that can be stored in the memory allocated by the current instance. Its value is assigned to the Capacity property. If the number of characters to be stored in the current instance exceeds this capacity value, the StringBuilder object allocates additional memory to store them.

The string value of this instance is set to String.Empty. If capacity is zero, the implementation-specific default capacity is used.

See also

Applies to

StringBuilder(String)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Initializes a new instance of the StringBuilder class using the specified string.

public:
 StringBuilder(System::String ^ value);
public StringBuilder (string value);
public StringBuilder (string? value);
new System.Text.StringBuilder : string -> System.Text.StringBuilder
Public Sub New (value As String)

Parameters

value
String

The 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).

Examples

The following example demonstrates how to call the StringBuilder constructor with the specified string.

String^ initialString = L"Initial string.";
StringBuilder^ stringBuilder = gcnew StringBuilder( initialString );
string initialString = "Initial string.";
StringBuilder stringBuilder = new StringBuilder(initialString);
let initialString = "Initial string."
let stringBuilder = StringBuilder initialString
Dim initialString As String = "Initial string."
Dim stringBuilder As New StringBuilder(initialString)

Remarks

If value is null, the new StringBuilder will contain the empty string (that is, it contains Empty).

Applies to

StringBuilder(Int32, Int32)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

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);
public StringBuilder (int capacity, int maxCapacity);
new System.Text.StringBuilder : int * int -> System.Text.StringBuilder
Public Sub New (capacity As Integer, maxCapacity As Integer)

Parameters

capacity
Int32

The suggested starting size of the StringBuilder.

maxCapacity
Int32

The maximum number of characters the current string can contain.

Exceptions

maxCapacity is less than one, capacity is less than zero, or capacity is greater than maxCapacity.

Examples

The following example demonstrates how to call the StringBuilder constructor with a specified capacity and maximum capacity.

int capacity = 255;
int maxCapacity = 1024;
StringBuilder^ stringBuilder = gcnew StringBuilder( capacity,maxCapacity );
int capacity = 255;
int maxCapacity = 1024;
StringBuilder stringBuilder = 
    new StringBuilder(capacity, maxCapacity);
let capacity = 255
let maxCapacity = 1024
let stringBuilder = StringBuilder(capacity, maxCapacity)
Dim capacity As Integer = 255
Dim maxCapacity As Integer = 1024
Dim stringBuilder As New StringBuilder(capacity, maxCapacity)

Remarks

The capacity parameter defines the maximum number of characters that can be stored in the memory allocated by the current instance. Its value is assigned to the Capacity property. If the number of characters to be stored in the current instance exceeds this capacity value, the StringBuilder object allocates additional memory to store them.

If capacity is zero, the implementation-specific default capacity is used.

The maxCapacity property defines the maximum number of characters that the current instance can hold. Its value is assigned to the MaxCapacity property. If the number of characters to be stored in the current instance exceeds this maxCapacity value, the StringBuilder object does not allocate additional memory, but instead throws an exception.

Notes to Callers

In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.

See also

Applies to

StringBuilder(String, Int32)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Initializes a new instance of the StringBuilder class using the specified string and capacity.

public:
 StringBuilder(System::String ^ value, int capacity);
public StringBuilder (string value, int capacity);
public StringBuilder (string? value, int capacity);
new System.Text.StringBuilder : string * int -> System.Text.StringBuilder
Public Sub New (value As String, capacity As Integer)

Parameters

value
String

The 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).

capacity
Int32

The suggested starting size of the StringBuilder.

Exceptions

capacity is less than zero.

Examples

The following example demonstrates how to call the StringBuilder constructor with an initial string and a specified capacity.

String^ initialString = L"Initial string. ";
int capacity = 255;
StringBuilder^ stringBuilder = gcnew StringBuilder(
   initialString,capacity );
string initialString = "Initial string. ";
int capacity = 255;
StringBuilder stringBuilder = 
    new StringBuilder(initialString, capacity);
let initialString = "Initial string. "
let capacity = 255
let stringBuilder = StringBuilder(initialString, capacity)
Dim initialString As String = "Initial string. "
Dim capacity As Integer = 255
Dim stringBuilder As New StringBuilder(initialString, capacity)

Remarks

The capacity parameter defines the maximum number of characters that can be stored in the memory allocated by the current instance. Its value is assigned to the Capacity property. If the number of characters to be stored in the current instance exceeds this capacity value, the StringBuilder object allocates additional memory to store them.

If capacity is zero, the implementation-specific default capacity is used.

See also

Applies to

StringBuilder(String, Int32, Int32, Int32)

Source:
StringBuilder.cs
Source:
StringBuilder.cs
Source:
StringBuilder.cs

Initializes a new instance of the StringBuilder class from the specified substring and capacity.

public:
 StringBuilder(System::String ^ value, int startIndex, int length, int capacity);
public StringBuilder (string value, int startIndex, int length, int capacity);
public StringBuilder (string? value, int startIndex, int length, int capacity);
new System.Text.StringBuilder : string * int * int * int -> System.Text.StringBuilder
Public Sub New (value As String, startIndex As Integer, length As Integer, capacity As Integer)

Parameters

value
String

The 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).

startIndex
Int32

The position within value where the substring begins.

length
Int32

The number of characters in the substring.

capacity
Int32

The suggested starting size of the StringBuilder.

Exceptions

capacity is less than zero.

-or-

startIndex plus length is not a position within value.

Examples

The following example demonstrates how to call the StringBuilder constructor with the specified string.

String^ initialString = L"Initial string for stringbuilder.";
int startIndex = 0;
int substringLength = 14;
int capacity = 255;
StringBuilder^ stringBuilder = gcnew StringBuilder(
   initialString,startIndex,substringLength,capacity );
string initialString = "Initial string for stringbuilder.";
int startIndex = 0;
int substringLength = 14;
int capacity = 255;
StringBuilder stringBuilder = new StringBuilder(initialString, 
    startIndex, substringLength, capacity);
let initialString = "Initial string for stringbuilder."
let startIndex = 0
let substringLength = 14
let capacity = 255

let stringBuilder =
    StringBuilder(initialString, startIndex, substringLength, capacity)
Dim initialString As String = "Initial string for stringbuilder."
Dim startIndex As Integer = 0
Dim substringLength As Integer = 14
Dim capacity As Integer = 255
Dim stringBuilder As New StringBuilder(initialString, _
    startIndex, substringLength, capacity)

Remarks

The capacity parameter defines the maximum number of characters that can be stored in the memory allocated by the current instance. Its value is assigned to the Capacity property. If the number of characters to be stored in the current instance exceeds this capacity value, the StringBuilder object allocates additional memory to store them.

If capacity is zero, the implementation-specific default capacity is used.

See also

Applies to