StringBuilder StringBuilder StringBuilder StringBuilder Constructors

定义

重载

StringBuilder() StringBuilder() StringBuilder()

初始化 StringBuilder 类的新实例。Initializes a new instance of the StringBuilder class.

StringBuilder(Int32) StringBuilder(Int32) StringBuilder(Int32) StringBuilder(Int32)

使用指定的容量初始化 StringBuilder 类的新实例。Initializes a new instance of the StringBuilder class using the specified capacity.

StringBuilder(String) StringBuilder(String) StringBuilder(String) StringBuilder(String)

使用指定的字符串初始化 StringBuilder 类的新实例。Initializes a new instance of the StringBuilder class using the specified string.

StringBuilder(Int32, Int32) StringBuilder(Int32, Int32) StringBuilder(Int32, Int32) 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(String, Int32) StringBuilder(String, Int32) StringBuilder(String, Int32)

使用指定的字符串和容量初始化 StringBuilder 类的新实例。Initializes a new instance of the StringBuilder class using the specified string and capacity.

StringBuilder(String, Int32, Int32, Int32) StringBuilder(String, Int32, Int32, Int32) StringBuilder(String, Int32, Int32, Int32) StringBuilder(String, Int32, Int32, Int32)

从指定的子字符串和容量初始化 StringBuilder 类的新实例。Initializes a new instance of the StringBuilder class from the specified substring and capacity.

StringBuilder() StringBuilder() 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(Int32) StringBuilder(Int32) 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 Int32 Int32 Int32

此实例的建议起始大小。The suggested starting size of this instance.

异常

示例

下面的示例演示如何使用指定的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(String) StringBuilder(String) 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 String String 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(Int32, Int32) StringBuilder(Int32, Int32) 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 Int32 Int32 Int32

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

maxCapacity
Int32 Int32 Int32 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 及更高版本StringBuilder中, 当通过StringBuilder(Int32, Int32)调用构造函数实例化对象时, StringBuilder实例的长度和容量超出其MaxCapacity知识产权.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(String, Int32) StringBuilder(String, Int32) 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 String String 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 Int32 Int32 Int32

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

异常

示例

下面的示例演示如何使用初始字符串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(String, Int32, Int32, Int32) StringBuilder(String, Int32, Int32, Int32) 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 String String 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 Int32 Int32 Int32

value 中子字符串开始的位置。The position within value where the substring begins.

length
Int32 Int32 Int32 Int32

子字符串中的字符数。The number of characters in the substring.

capacity
Int32 Int32 Int32 Int32

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

异常

capacity 小于零。capacity is less than zero.

-or- startIndexlength 不是 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.

另请参阅

适用于