Skip to main content
StringToCharArray Method
 

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

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

public char[] ToCharArray(
	int startIndex,
	int length
)
public:
array<wchar_t>^ ToCharArray(
	int startIndex,
	int length
)
member ToCharArray : 
        startIndex:int *
        length:int -> char[]
Public Function ToCharArray (
	startIndex As Integer,
	length As Integer
) As Char()

Parameters

startIndex
Type:

The starting position of a substring in this instance.

length
Type:

The length of the substring in this instance.

Return Value

Type:

A Unicode character array whose elements are the length number of characters in this instance starting from character position startIndex.

Exception Condition
ArgumentOutOfRangeException

startIndex or length is less than zero.

-or-

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 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 null or an empty string (""), the returned array is empty and has a zero length.

To create a byte array that contains the encoded characters in a portion of a string, instantiate the appropriate Encoding object and call its GetBytes method. Some of the standard encodings available in the .NET Framework include the following:

Encoding

Object

ASCII

ASCIIEncoding

UTF-7

UTF7Encoding

UTF-8

UTF8Encoding

UTF-16

UnicodeEncoding

UTF-32

UTF32Encoding

For more information, see Character Encoding in the .NET Framework.

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)
using System;

class Sample {
    public static void Main() {
    string str = "012wxyz789";
    char[] arr;

    arr = str.ToCharArray(3, 4);
    Console.Write("The letters in '{0}' are: '", str);
    Console.Write(arr);
    Console.WriteLine("'");
    Console.WriteLine("Each letter in '{0}' is:", str);
    foreach (char c in arr)
        Console.WriteLine(c);
    }
}
/*
This example produces the following results:
The letters in '012wxyz789' are: 'wxyz'
Each letter in '012wxyz789' is:
w
x
y
z
*/
' 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.Write(arr)
      Console.WriteLine("'")
      Console.WriteLine("Each letter in '{0}' is:", str)
      Dim c As Char
      For Each c In arr
         Console.WriteLine(c)
      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:
'w
'x
'y
'z
'
// Sample for String::ToCharArray(Int32, Int32)
using namespace System;
using namespace System::Collections;
int main()
{
   String^ str = "012wxyz789";
   array<Char>^arr;
   arr = str->ToCharArray( 3, 4 );
   Console::Write( "The letters in '{0}' are: '", str );
   Console::Write( arr );
   Console::WriteLine( "'" );
   Console::WriteLine( "Each letter in '{0}' is:", str );
   IEnumerator^ myEnum = arr->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Char c =  safe_cast<Char>(myEnum->Current);
      Console::WriteLine( c );
   }
}

/*
This example produces the following results:
The letters in '012wxyz789' are: 'wxyz'
Each letter in '012wxyz789' is:
w
x
y
z
*/
Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top