The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.ToCharArray Method (Int32, Int32)

Copies the characters in a specified substring in this instance to a Unicode character array.

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

Public Function ToCharArray ( _
	startIndex As Integer, _
	length As Integer _
) As Char()


Type: System.Int32

The starting position of a substring in this instance.

Type: System.Int32

The length of the substring in this instance.

Return Value

Type: System.Char()
A Unicode character array whose elements are the length number of characters in this instance starting from character position startIndex.


startIndex or length is less than zero.


startIndex plus length is greater than the length of this instance.

This method copies the characters in a portion of a string to a character array. To create a string from a range of characters in a character array, call the String(Char(), Int32, Int32) constructor.

The startIndex parameter is zero-based. That is, the index of the first character in the string instance is zero.

If length is zero, the returned array is empty and has a zero length. If this instance is Nothing or an empty string (""), the returned array is empty and has a zero length.

The following example converts a substring within a string to an array of characters, then enumerates and displays the elements of the array.

' Sample for String.ToCharArray(Int32, Int32) 
Imports System

Class Sample

   Public Shared Sub Main()
      Dim str As String = "012wxyz789" 
      Dim arr() As Char

      arr = str.ToCharArray(3, 4)
      Console.Write("The letters in '{0}' are: '", str)
      Console.WriteLine("Each letter in '{0}' is:", str)
      Dim c As Char 
      For Each c In arr
      Next c
   End Sub 'Main
End Class 'Sample

'This example produces the following results: 
'The letters in '012wxyz789' are: 'wxyz' 
'Each letter in '012wxyz789' is: 

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
© 2015 Microsoft