This documentation is archived and is not being maintained.

String.ToCharArray Method

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

Overload List

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function ToCharArray() As Char()
[C#] public char[] ToCharArray();
[C++] public: __wchar_t ToCharArray() __gc[];
[JScript] public function ToCharArray() : Char[];

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

[Visual Basic] Overloads Public Function ToCharArray(Integer, Integer) As Char()
[C#] public char[] ToCharArray(int, int);
[C++] public: __wchar_t ToCharArray(int, int) __gc[];
[JScript] public function ToCharArray(int, int) : Char[];

Example

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ToCharArray. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' 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
'

[C#] 
// 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
*/

[C++] 
// Sample for String::ToCharArray(Int32, Int32)
#using <mscorlib.dll>

using namespace System;
using namespace System::Collections;

int main()
{
   String* str = S"012wxyz789";
   Char arr[];

   arr = str->ToCharArray(3, 4);
   Console::Write(S"The letters in '{0}' are: '", str);
   Console::Write(arr);
   Console::WriteLine(S"'");
   Console::WriteLine(S"Each letter in '{0}' is:", str);
   IEnumerator* myEnum = arr->GetEnumerator();
   while (myEnum->MoveNext()) {
      Char c = *__try_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
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

String Class | String Members | System Namespace

Show: