StringBuilder.Insert Method (Int32, Single)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

Syntax

Public Function Insert ( _
    index As Integer, _
    value As Single _
) As StringBuilder
public StringBuilder Insert(
    int index,
    float value
)

Parameters

  • index
    Type: System..::.Int32
    The position in this instance where insertion begins.

Return Value

Type: System.Text..::.StringBuilder
A reference to this instance after the insert operation has completed.

Exceptions

Exception Condition
ArgumentOutOfRangeException

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

OutOfMemoryException

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 version 4, the method throws an OutOfMemoryException.

Examples

The following example demonstrates the Insert method.

Imports System.Text

Class Example
   '                                 index: 012345
   Private Shared initialValue As String = "--[]--"
   Private Shared sb As StringBuilder

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim xyz As String = "xyz"
      Dim abc As Char() = {"a"c, "b"c, "c"c}
      Dim star As Char = "*"c
      Dim obj As [Object] = 0

      Dim xBool As Boolean = True
      Dim xByte As Byte = 1
      Dim xInt16 As Short = 2
      Dim xInt32 As Integer = 3
      Dim xInt64 As Long = 4
      Dim xDecimal As [Decimal] = 5
      Dim xSingle As Single = 6.6F
      Dim xDouble As Double = 7.7

      ' The following types are not CLS-compliant.
      ' Dim xUInt16 As System.UInt16 = 8 
      ' Dim xUInt32 As System.UInt32 = 9
      ' Dim xUInt64 As System.UInt64 = 10 
      ' Dim xSByte As System.SByte = - 11
      '
      outputBlock.Text &= "StringBuilder.Insert method" & vbCrLf
      sb = New StringBuilder(initialValue)

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

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

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

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

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

      sb.Insert(3, xBool)     ' True
      Show(outputBlock, 6, sb)

      sb.Insert(3, obj)       ' 0
      Show(outputBlock, 7, sb)

      sb.Insert(3, xByte)     ' 1
      Show(outputBlock, 8, sb)

      sb.Insert(3, xInt16)    ' 2
      Show(outputBlock, 9, sb)

      sb.Insert(3, xInt32)    ' 3
      Show(outputBlock, 10, sb)

      sb.Insert(3, xInt64)    ' 4
      Show(outputBlock, 11, sb)

      sb.Insert(3, xDecimal)  ' 5
      Show(outputBlock, 12, sb)

      sb.Insert(3, xSingle)   ' 6.6
      Show(outputBlock, 13, sb)

      sb.Insert(3, xDouble)   ' 7.7
      Show(outputBlock, 14, sb)

      ' The following Insert methods are not CLS-compliant.
      ' sb.Insert(3, xUInt16) ' 8
      ' sb.Insert(3, xUInt32) ' 9
      ' sb.Insert(3, xUInt64) ' 10
      ' sb.Insert(3, xSByte)  ' -11

   End Sub 'Main

   Public Shared Sub Show(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal overloadNumber As Integer, ByVal sbs As StringBuilder)
      outputBlock.Text += String.Format("{0,2:G} = {1}", overloadNumber, sbs.ToString()) & vbCrLf
      sb = New StringBuilder(initialValue)
   End Sub 'Show
End Class '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]--
'
using System;
using System.Text;

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

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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;
      //
      outputBlock.Text += "StringBuilder.Insert method" + "\n";
      sb = new StringBuilder(initialValue);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

   public static void Show(System.Windows.Controls.TextBlock outputBlock, int overloadNumber, StringBuilder sbs)
   {
      outputBlock.Text += String.Format("{0,2:G} = {1}", overloadNumber, sbs.ToString()) + "\n";
      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]--

*/

Version Information

Windows Phone OS

Supported in: 8.1, 8.0

See Also

Reference

StringBuilder Class

Insert Overload

System.Text Namespace

Single