Export (0) Print
Expand All

StringBuilder.Insert Method (Int32, String, Int32)

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

Namespace: System.Text
Assembly: mscorlib (in mscorlib.dll)

public StringBuilder Insert (
	int index,
	string value,
	int count
)
public StringBuilder Insert (
	int index, 
	String value, 
	int count
)
public function Insert (
	index : int, 
	value : String, 
	count : int
) : StringBuilder

Parameters

index

The position in this instance where insertion begins.

value

The string to insert.

count

The number of times to insert value.

Return Value

A reference to this instance after insertion has completed.

Exception typeCondition

ArgumentOutOfRangeException

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

-or-

count is less than zero.

OutOfMemoryException

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

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 a null reference (Nothing in Visual Basic), value is not a null reference (Nothing in Visual Basic) but its length is zero, or count is zero.

The following code example demonstrates the Insert method.

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]--

*/

// This example demonstrates StringBuilder.Insert()
import System.*;
import System.Text.*;

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

    public static void main(String[] args)
    {
        String xyz = "xyz";
        char abc[] =  { 'a', 'b', 'c' };
        char star = '*';
        Object obj = new Integer(0);

        boolean xBool = true;
        ubyte xByte = 1;
        short xInt16 = 2;
        int xInt32 = 3;
        long xInt64 = 4;
        Decimal xDecimal = new Decimal(5);
        float xSingle = 6.6f;
        double xDouble = 7.7;
        
        // The following types are not CLS-compliant.
        int xUInt16 = 8;
        int xUInt32 = 9;
        long xUInt64 = 10;
        byte 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);
    } //main

    //
    public static void Show(int overloadNumber, StringBuilder sbs)
    {
        Console.WriteLine("{0,2:G} = {1}", 
            System.Convert.ToString(overloadNumber), sbs.ToString());
        sb = new StringBuilder(initialValue);
    } //Show
} //Sample
/*
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]--

*/

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft