StringBuilder 建構函式

定義

初始化 StringBuilder 類別的新執行個體。Initializes a new instance of the StringBuilder class.

多載

StringBuilder()

初始化 StringBuilder 類別的新執行個體。Initializes a new instance of the StringBuilder class.

StringBuilder(Int32)

使用指定的容量來初始化 StringBuilder 類別的新執行個體。Initializes a new instance of the StringBuilder class using the specified capacity.

StringBuilder(String)

以指定的字串初始化 StringBuilder 類別的新執行個體。Initializes a new instance of the StringBuilder class using the specified string.

StringBuilder(Int32, Int32)

初始化 StringBuilder 類別的新執行個體,將從指定的容量開始並且可以增加至指定的最大值。Initializes a new instance of the StringBuilder class that starts with a specified capacity and can grow to a specified maximum.

StringBuilder(String, Int32)

使用指定的字串和容量,來初始化 StringBuilder 類別的新執行個體。Initializes a new instance of the StringBuilder class using the specified string and capacity.

StringBuilder(String, Int32, Int32, Int32)

以指定的子字串和容量初始化 StringBuilder 類別的新執行個體。Initializes a new instance of the StringBuilder class from the specified substring and capacity.

StringBuilder()

初始化 StringBuilder 類別的新執行個體。Initializes a new instance of the StringBuilder class.

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

範例

下列範例示範如何呼叫不含參數StringBuilder的函式。The following example demonstrates how to call the StringBuilder constructor with no parameters.

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

備註

這個實例的字串值設定為String.Empty,並將容量設定為特定執行方式的預設容量。The string value of this instance is set to String.Empty, and the capacity is set to the implementation-specific default capacity.

StringBuilder(Int32)

使用指定的容量來初始化 StringBuilder 類別的新執行個體。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)

參數

capacity
Int32

這個執行個體建議的起始大小。The suggested starting size of this instance.

例外狀況

capacity 小於零。capacity is less than zero.

範例

下列範例示範如何使用指定的容量StringBuilder呼叫此函式。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);
Dim capacity As Integer = 255
Dim stringBuilder As New StringBuilder(capacity)

備註

capacity參數會定義可儲存在目前實例所配置記憶體中的最大字元數。The capacity parameter defines the maximum number of characters that can be stored in the memory allocated by the current instance. 其值會指派Capacity給屬性。Its value is assigned to the Capacity property. 如果目前實例中儲存的字元數目超過此capacity值,則StringBuilder物件會配置額外的記憶體來儲存它們。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.

這個實例的字串值設定為String.EmptyThe string value of this instance is set to String.Empty. 如果capacity為零,則會使用執行特定的預設容量。If capacity is zero, the implementation-specific default capacity is used.

另請參閱

StringBuilder(String)

以指定的字串初始化 StringBuilder 類別的新執行個體。Initializes a new instance of the StringBuilder class using the specified string.

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

參數

value
String

用來初始化執行個體值的字串。The string used to initialize the value of the instance. 如果 valuenull,新的 StringBuilder 將包含空字串 (即包含 Empty)。If value is null, the new StringBuilder will contain the empty string (that is, it contains Empty).

範例

下列範例示範如何使用指定的字串StringBuilder呼叫此函式。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);
Dim initialString As String = "Initial string."
Dim stringBuilder As New StringBuilder(initialString)

備註

如果 valuenull,新的 StringBuilder 將包含空字串 (即包含 Empty)。If value is null, the new StringBuilder will contain the empty string (that is, it contains Empty).

StringBuilder(Int32, Int32)

初始化 StringBuilder 類別的新執行個體,將從指定的容量開始並且可以增加至指定的最大值。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)

參數

capacity
Int32

StringBuilder 建議的起始大小。The suggested starting size of the StringBuilder.

maxCapacity
Int32

目前字串可以包含的最大字元數。The maximum number of characters the current string can contain.

例外狀況

maxCapacity 小於一,capacity 小於零,或 capacity 大於 maxCapacitymaxCapacity is less than one, capacity is less than zero, or capacity is greater than maxCapacity.

範例

下列範例示範如何使用指定的容量StringBuilder和最大容量來呼叫此函數。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);
Dim capacity As Integer = 255
Dim maxCapacity As Integer = 1024
Dim stringBuilder As New StringBuilder(capacity, maxCapacity)

備註

capacity參數會定義可儲存在目前實例所配置記憶體中的最大字元數。The capacity parameter defines the maximum number of characters that can be stored in the memory allocated by the current instance. 其值會指派Capacity給屬性。Its value is assigned to the Capacity property. 如果目前實例中儲存的字元數目超過此capacity值,則StringBuilder物件會配置額外的記憶體來儲存它們。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.

如果capacity為零,則會使用執行特定的預設容量。If capacity is zero, the implementation-specific default capacity is used.

maxCapacity屬性會定義目前實例可以保存的最大字元數。The maxCapacity property defines the maximum number of characters that the current instance can hold. 其值會指派MaxCapacity給屬性。Its value is assigned to the MaxCapacity property. 如果目前實例中儲存的字元數超過此maxCapacity值,則StringBuilder物件不會配置額外的記憶體,而是會擲回例外狀況。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.

給呼叫者的注意事項

在 .net Core 和 .NET Framework 4.0 和更新版本中,當您藉由呼叫StringBuilderStringBuilder(Int32, Int32)函式來具現化物件時, StringBuilder實例的長度和容量都可以成長超過其MaxCapacity的值property.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. 當您呼叫Append(String)AppendFormat(String, Object)方法來附加小型字串時,可能會發生這種情況。This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.

另請參閱

StringBuilder(String, Int32)

使用指定的字串和容量,來初始化 StringBuilder 類別的新執行個體。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);
new System.Text.StringBuilder : string * int -> System.Text.StringBuilder
Public Sub New (value As String, capacity As Integer)

參數

value
String

用來初始化執行個體值的字串。The string used to initialize the value of the instance. 如果 valuenull,新的 StringBuilder 將包含空字串 (即包含 Empty)。If value is null, the new StringBuilder will contain the empty string (that is, it contains Empty).

capacity
Int32

StringBuilder 建議的起始大小。The suggested starting size of the StringBuilder.

例外狀況

capacity 小於零。capacity is less than zero.

範例

下列範例示範如何使用初始字串和StringBuilder指定的容量呼叫此函式。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);
Dim initialString As String = "Initial string. "
Dim capacity As Integer = 255
Dim stringBuilder As New StringBuilder(initialString, capacity)

備註

capacity參數會定義可儲存在目前實例所配置記憶體中的最大字元數。The capacity parameter defines the maximum number of characters that can be stored in the memory allocated by the current instance. 其值會指派Capacity給屬性。Its value is assigned to the Capacity property. 如果目前實例中儲存的字元數目超過此capacity值,則StringBuilder物件會配置額外的記憶體來儲存它們。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.

如果capacity為零,則會使用執行特定的預設容量。If capacity is zero, the implementation-specific default capacity is used.

另請參閱

StringBuilder(String, Int32, Int32, Int32)

以指定的子字串和容量初始化 StringBuilder 類別的新執行個體。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);
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)

參數

value
String

字串,包含用來初始化此執行個體值的子字串。The string that contains the substring used to initialize the value of this instance. 如果 valuenull,新的 StringBuilder 將包含空字串 (即包含 Empty)。If value is null, the new StringBuilder will contain the empty string (that is, it contains Empty).

startIndex
Int32

value 內子字串開始的位置。The position within value where the substring begins.

length
Int32

子字串中的字元數。The number of characters in the substring.

capacity
Int32

StringBuilder 建議的起始大小。The suggested starting size of the StringBuilder.

例外狀況

capacity 小於零。capacity is less than zero.

-或--or- startIndex 加上 length 不是 value 內的位置。startIndex plus length is not a position within value.

範例

下列範例示範如何使用指定的字串StringBuilder呼叫此函式。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);
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)

備註

capacity參數會定義可儲存在目前實例所配置記憶體中的最大字元數。The capacity parameter defines the maximum number of characters that can be stored in the memory allocated by the current instance. 其值會指派Capacity給屬性。Its value is assigned to the Capacity property. 如果目前實例中儲存的字元數目超過此capacity值,則StringBuilder物件會配置額外的記憶體來儲存它們。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.

如果capacity為零,則會使用執行特定的預設容量。If capacity is zero, the implementation-specific default capacity is used.

另請參閱

適用於