StringBuilder.Insert Method

Definition

Inserts the string representation of a specified object into this instance at a specified character position.

Overloads

Insert(Int32, SByte)

Inserts the string representation of a specified 8-bit signed integer into this instance at the specified character position.

Insert(Int32, Char[], Int32, Int32)

Inserts the string representation of a specified subarray of Unicode characters into this instance at the specified character position.

Insert(Int32, String, Int32)

Inserts one or more copies of a specified string into this instance at the specified character position.

Insert(Int32, UInt64)

Inserts the string representation of a 64-bit unsigned integer into this instance at the specified character position.

Insert(Int32, UInt32)

Inserts the string representation of a 32-bit unsigned integer into this instance at the specified character position.

Insert(Int32, UInt16)

Inserts the string representation of a 16-bit unsigned integer into this instance at the specified character position.

Insert(Int32, String)

Inserts a string into this instance at the specified character position.

Insert(Int32, Single)

Inserts the string representation of a single-precision floating point number into this instance at the specified character position.

Insert(Int32, ReadOnlySpan<Char>)

Inserts the sequence of characters into this instance at the specified character position.

Insert(Int32, Int16)

Inserts the string representation of a specified 16-bit signed integer into this instance at the specified character position.

Insert(Int32, Int64)

Inserts the string representation of a 64-bit signed integer into this instance at the specified character position.

Insert(Int32, Int32)

Inserts the string representation of a specified 32-bit signed integer into this instance at the specified character position.

Insert(Int32, Object)

Inserts the string representation of an object into this instance at the specified character position.

Insert(Int32, Double)

Inserts the string representation of a double-precision floating-point number into this instance at the specified character position.

Insert(Int32, Decimal)

Inserts the string representation of a decimal number into this instance at the specified character position.

Insert(Int32, Char[])

Inserts the string representation of a specified array of Unicode characters into this instance at the specified character position.

Insert(Int32, Char)

Inserts the string representation of a specified Unicode character into this instance at the specified character position.

Insert(Int32, Byte)

Inserts the string representation of a specified 8-bit unsigned integer into this instance at the specified character position.

Insert(Int32, Boolean)

Inserts the string representation of a Boolean value into this instance at the specified character position.

Examples

The following example demonstrates the Insert method.

C#
using System;
using System.Text;

class Sample
{
//                         index: 012345
    static string initialValue = "--[]--";
    static StringBuilder sb;

    public static void Main()
    {
    string      xyz       = "xyz";
    char[]      abc       = {'a', 'b', 'c'};
    char        star      = '*';
    Object 	obj       = 0;

    bool        xBool     = true;
    byte        xByte     = 1;
    short       xInt16    = 2;
    int         xInt32    = 3;
    long        xInt64    = 4;
    Decimal     xDecimal  = 5;
    float       xSingle   = 6.6F;
    double      xDouble   = 7.7;

// The following types are not CLS-compliant.
    ushort      xUInt16   = 8;
    uint        xUInt32   = 9;
    ulong       xUInt64   = 10;
    sbyte       xSByte    = -11;
//
    Console.WriteLine("StringBuilder.Insert method");
    sb = new StringBuilder(initialValue);

    sb.Insert(3, xyz, 2);
    Show(1, sb);

    sb.Insert(3, xyz);
    Show(2, sb);

    sb.Insert(3, star);
    Show(3, sb);

    sb.Insert(3, abc);
    Show(4, sb);

    sb.Insert(3, abc, 1, 2);
    Show(5, sb);

    sb.Insert(3, xBool);     // True
    Show(6, sb);

    sb.Insert(3, obj);       // 0
    Show(7, sb);

    sb.Insert(3, xByte);     // 1
    Show(8, sb);

    sb.Insert(3, xInt16);    // 2
    Show(9, sb);

    sb.Insert(3, xInt32);    // 3
    Show(10, sb);

    sb.Insert(3, xInt64);    // 4
    Show(11, sb);

    sb.Insert(3, xDecimal);  // 5
    Show(12, sb);

    sb.Insert(3, xSingle);   // 6.6
    Show(13, sb);

    sb.Insert(3, xDouble);   // 7.7
    Show(14, sb);

// The following Insert methods are not CLS-compliant.
    sb.Insert(3, xUInt16);   // 8
    Show(15, sb);

    sb.Insert(3, xUInt32);   // 9
    Show(16, sb);

    sb.Insert(3, xUInt64);   // 10
    Show(17, sb);

    sb.Insert(3, xSByte);    // -11
    Show(18, sb);
//
    }

    public static void Show(int overloadNumber, StringBuilder sbs)
    {
    Console.WriteLine("{0,2:G} = {1}", overloadNumber, sbs.ToString());
    sb = new StringBuilder(initialValue);
    }
}
/*
This example produces the following results:

StringBuilder.Insert method
 1 = --[xyzxyz]--
 2 = --[xyz]--
 3 = --[*]--
 4 = --[abc]--
 5 = --[bc]--
 6 = --[True]--
 7 = --[0]--
 8 = --[1]--
 9 = --[2]--
10 = --[3]--
11 = --[4]--
12 = --[5]--
13 = --[6.6]--
14 = --[7.7]--
15 = --[8]--
16 = --[9]--
17 = --[10]--
18 = --[-11]--

*/

Insert(Int32, SByte)

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

Important

This API is not CLS-compliant.

Inserts the string representation of a specified 8-bit signed integer into this instance at the specified character position.

C#
[System.CLSCompliant(false)]
public System.Text.StringBuilder Insert(int index, sbyte value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
SByte

The value to insert.

Returns

A reference to this instance after the insert operation has completed.

Attributes

Exceptions

index is less than zero or greater than the length of this instance.

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

SByte.ToString is used to get a string representation of value. Existing characters are shifted to make room for the new text. The capacity is adjusted as needed.

Notes to Callers

In the .NET Framework 3.5 Service Pack 1 and earlier versions, calls to this method threw an ArgumentOutOfRangeException if inserting value would cause the object's total length to exceed MaxCapacity. Starting with the .NET Framework 4, the method throws an OutOfMemoryException.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, Char[], Int32, Int32)

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

Inserts the string representation of a specified subarray of Unicode characters into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, char[] value, int startIndex, int charCount);
C#
public System.Text.StringBuilder Insert(int index, char[]? value, int startIndex, int charCount);

Parameters

index
Int32

The position in this instance where insertion begins.

value
Char[]

A character array.

startIndex
Int32

The starting index within value.

charCount
Int32

The number of characters to insert.

Returns

A reference to this instance after the insert operation has completed.

Exceptions

value is null, and startIndex and charCount are not zero.

index, startIndex, or charCount is less than zero.

-or-

index is greater than the length of this instance.

-or-

startIndex plus charCount is not a position within value.

-or-

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, String, Int32)

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

Inserts one or more copies of a specified string into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, string value, int count);
C#
public System.Text.StringBuilder Insert(int index, string? value, int count);

Parameters

index
Int32

The position in this instance where insertion begins.

value
String

The string to insert.

count
Int32

The number of times to insert value.

Returns

A reference to this instance after insertion has completed.

Exceptions

index is less than zero or greater than the current length of this instance.

-or-

count is less than zero.

The current length of this StringBuilder object plus the length of value times count exceeds MaxCapacity.

Remarks

Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

This StringBuilder object is not changed if value is null, value is not null but its length is zero, or count is zero.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, UInt64)

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

Important

This API is not CLS-compliant.

Inserts the string representation of a 64-bit unsigned integer into this instance at the specified character position.

C#
[System.CLSCompliant(false)]
public System.Text.StringBuilder Insert(int index, ulong value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
UInt64

The value to insert.

Returns

A reference to this instance after the insert operation has completed.

Attributes

Exceptions

index is less than zero or greater than the length of this instance.

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

UInt64.ToString is used to get a string representation of value. Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

Notes to Callers

In the .NET Framework 3.5 Service Pack 1 and earlier versions, calls to this method threw an ArgumentOutOfRangeException if inserting value would cause the object's total length to exceed MaxCapacity. Starting with the .NET Framework 4, the method throws an OutOfMemoryException.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, UInt32)

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

Important

This API is not CLS-compliant.

Inserts the string representation of a 32-bit unsigned integer into this instance at the specified character position.

C#
[System.CLSCompliant(false)]
public System.Text.StringBuilder Insert(int index, uint value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
UInt32

The value to insert.

Returns

A reference to this instance after the insert operation has completed.

Attributes

Exceptions

index is less than zero or greater than the length of this instance.

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

UInt32.ToString is used to get a string representation of value. Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

Notes to Callers

In the .NET Framework 3.5 Service Pack 1 and earlier versions, calls to this method threw an ArgumentOutOfRangeException if inserting value would cause the object's total length to exceed MaxCapacity. Starting with the .NET Framework 4, the method throws an OutOfMemoryException.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, UInt16)

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

Important

This API is not CLS-compliant.

Inserts the string representation of a 16-bit unsigned integer into this instance at the specified character position.

C#
[System.CLSCompliant(false)]
public System.Text.StringBuilder Insert(int index, ushort value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
UInt16

The value to insert.

Returns

A reference to this instance after the insert operation has completed.

Attributes

Exceptions

index is less than zero or greater than the length of this instance.

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

UInt16.ToString is used to get a string representation of value. Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

Notes to Callers

In the .NET Framework 3.5 Service Pack 1 and earlier versions, calls to this method threw an ArgumentOutOfRangeException if inserting value would cause the object's total length to exceed MaxCapacity. Starting with the .NET Framework 4, the method throws an OutOfMemoryException.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, String)

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

Inserts a string into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, string value);
C#
public System.Text.StringBuilder Insert(int index, string? value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
String

The string to insert.

Returns

A reference to this instance after the insert operation has completed.

Exceptions

index is less than zero or greater than the current length of this instance.

-or-

The current length of this StringBuilder object plus the length of value exceeds MaxCapacity.

Remarks

Existing characters are shifted to make room for the new text. The capacity is adjusted as needed.

This instance of StringBuilder is not changed if value is null, or value is not null but its length is zero.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, Single)

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

Inserts the string representation of a single-precision floating point number into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, float value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
Single

The value to insert.

Returns

A reference to this instance after the insert operation has completed.

Exceptions

index is less than zero or greater than the length of this instance.

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

Single.ToString is used to get a string representation of value. Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

Notes to Callers

In the .NET Framework 3.5 Service Pack 1 and earlier versions, calls to this method threw an ArgumentOutOfRangeException if inserting value would cause the object's total length to exceed MaxCapacity. Starting with the .NET Framework 4, the method throws an OutOfMemoryException.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, ReadOnlySpan<Char>)

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

Inserts the sequence of characters into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, ReadOnlySpan<char> value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
ReadOnlySpan<Char>

The character span to insert.

Returns

A reference to this instance after the insert operation has completed.

Remarks

The existing characters are shifted to make room for the character sequence in the value to insert it. The capacity is adjusted as needed.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Insert(Int32, Int16)

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

Inserts the string representation of a specified 16-bit signed integer into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, short value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
Int16

The value to insert.

Returns

A reference to this instance after the insert operation has completed.

Exceptions

index is less than zero or greater than the length of this instance.

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

Int16.ToString is used to get a string representation of value. Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

Notes to Callers

In the .NET Framework 3.5 Service Pack 1 and earlier versions, calls to this method threw an ArgumentOutOfRangeException if inserting value would cause the object's total length to exceed MaxCapacity. Starting with the .NET Framework 4, the method throws an OutOfMemoryException.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, Int64)

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

Inserts the string representation of a 64-bit signed integer into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, long value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
Int64

The value to insert.

Returns

A reference to this instance after the insert operation has completed.

Exceptions

index is less than zero or greater than the length of this instance.

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

Int64.ToString is used to get a string representation of value. Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

Notes to Callers

In the .NET Framework 3.5 Service Pack 1 and earlier versions, calls to this method threw an ArgumentOutOfRangeException if inserting value would cause the object's total length to exceed MaxCapacity. Starting with the .NET Framework 4, the method throws an OutOfMemoryException.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, Int32)

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

Inserts the string representation of a specified 32-bit signed integer into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, int value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
Int32

The value to insert.

Returns

A reference to this instance after the insert operation has completed.

Exceptions

index is less than zero or greater than the length of this instance.

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

Int32.ToString is used to get a string representation of value. Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

Notes to Callers

In the .NET Framework 3.5 Service Pack 1 and earlier versions, calls to this method threw an ArgumentOutOfRangeException if inserting value would cause the object's total length to exceed MaxCapacity. Starting with the .NET Framework 4, the method throws an OutOfMemoryException.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, Object)

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

Inserts the string representation of an object into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, object value);
C#
public System.Text.StringBuilder Insert(int index, object? value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
Object

The object to insert, or null.

Returns

A reference to this instance after the insert operation has completed.

Exceptions

index is less than zero or greater than the length of this instance.

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

Object.ToString is used to get a string representation of value. Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

If value is null, the value of this instance is unchanged.

Notes to Callers

In the .NET Framework 3.5 Service Pack 1 and earlier versions, calls to this method threw an ArgumentOutOfRangeException if inserting value would cause the object's total length to exceed MaxCapacity. Starting with the .NET Framework 4, the method throws an OutOfMemoryException.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, Double)

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

Inserts the string representation of a double-precision floating-point number into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, double value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
Double

The value to insert.

Returns

A reference to this instance after the insert operation has completed.

Exceptions

index is less than zero or greater than the length of this instance.

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

Double.ToString is used to get a string representation of value. Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

Notes to Callers

In the .NET Framework 3.5 Service Pack 1 and earlier versions, calls to this method threw an ArgumentOutOfRangeException if inserting value would cause the object's total length to exceed MaxCapacity. Starting with the .NET Framework 4, the method throws an OutOfMemoryException.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, Decimal)

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

Inserts the string representation of a decimal number into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, decimal value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
Decimal

The value to insert.

Returns

A reference to this instance after the insert operation has completed.

Exceptions

index is less than zero or greater than the length of this instance.

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

Decimal.ToString is used to get a string representation of value. Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

Notes to Callers

In the .NET Framework 3.5 Service Pack 1 and earlier versions, calls to this method threw an ArgumentOutOfRangeException if inserting value would cause the object's total length to exceed MaxCapacity. Starting with the .NET Framework 4, the method throws an OutOfMemoryException.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, Char[])

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

Inserts the string representation of a specified array of Unicode characters into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, char[] value);
C#
public System.Text.StringBuilder Insert(int index, char[]? value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
Char[]

The character array to insert.

Returns

A reference to this instance after the insert operation has completed.

Exceptions

index is less than zero or greater than the length of this instance.

-or-

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

If value is null, the StringBuilder is not changed.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, Char)

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

Inserts the string representation of a specified Unicode character into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, char value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
Char

The value to insert.

Returns

A reference to this instance after the insert operation has completed.

Exceptions

index is less than zero or greater than the length of this instance.

-or-

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

Char.ToString is used to get a string representation of value. Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, Byte)

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

Inserts the string representation of a specified 8-bit unsigned integer into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, byte value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
Byte

The value to insert.

Returns

A reference to this instance after the insert operation has completed.

Exceptions

index is less than zero or greater than the length of this instance.

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

Byte.ToString is used to get a string representation of value. Existing characters are shifted to make room for the new text. The capacity of this instance is adjusted as needed.

Notes to Callers

In the .NET Framework 3.5 Service Pack 1 and earlier versions, calls to this method threw an ArgumentOutOfRangeException if inserting value would cause the object's total length to exceed MaxCapacity. Starting with the .NET Framework 4, the method throws an OutOfMemoryException.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Insert(Int32, Boolean)

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

Inserts the string representation of a Boolean value into this instance at the specified character position.

C#
public System.Text.StringBuilder Insert(int index, bool value);

Parameters

index
Int32

The position in this instance where insertion begins.

value
Boolean

The value to insert.

Returns

A reference to this instance after the insert operation has completed.

Exceptions

index is less than zero or greater than the length of this instance.

Enlarging the value of this instance would exceed MaxCapacity.

Remarks

Boolean.ToString is used to get a string representation of value. Existing characters are shifted to make room for the new text. The capacity is adjusted as needed.

Notes to Callers

In the .NET Framework 3.5 Service Pack 1 and earlier versions, calls to this method threw an ArgumentOutOfRangeException if inserting value would cause the object's total length to exceed MaxCapacity. Starting with the .NET Framework 4, the method throws an OutOfMemoryException.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0