Export (0) Print
Expand All

StringBuilder.Insert Method (Int32, Int16)

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

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

'Declaration
Public Function Insert ( _
	index As Integer, _
	value As Short _
) As StringBuilder

Parameters

index
Type: System.Int32

The position in this instance where insertion begins.

value
Type: System.Int16

The value to insert.

Return Value

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

ExceptionCondition
ArgumentOutOfRangeException

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

OutOfMemoryException

Enlarging the value of this instance would exceed MaxCapacity.

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.

The following example demonstrates the Insert method.

Imports System
Imports System.Text

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

   Public Shared Sub Main()
      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 
      '
      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 
      ' sb.Insert(3, xUInt32) ' 9 
      ' sb.Insert(3, xUInt64) ' 10 
      ' sb.Insert(3, xSByte)  ' -11 

   End Sub 'Main

   Public Shared Sub Show(overloadNumber As Integer, sbs As StringBuilder)
      Console.WriteLine("{0,2:G} = {1}", overloadNumber, sbs.ToString())
      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]-- 
'

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft