Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
String Class
String Methods
 ToCharArray Method
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
String..::.ToCharArray Method

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Function ToCharArray As Char()
Visual Basic (Usage)
Dim instance As String
Dim returnValue As Char()

returnValue = instance.ToCharArray()
C#
public char[] ToCharArray()
Visual C++
public:
array<wchar_t>^ ToCharArray()
JScript
public function ToCharArray() : char[]

Return Value

Type: array<System..::.Char>[]()[]
A Unicode character array whose elements are the individual characters of this instance. If this instance is an empty string, the returned array is empty and has a zero length.

The following example demonstrates how to easily create a Unicode character array from a String. The array is then used with the Split method.

Visual Basic
Public Class StringSplit2
   Public Shared Sub Main()

      Dim delimStr As String = " ,.:"
      Dim delimiter As Char() = delimStr.ToCharArray()
      Dim words As String = "one two,three:four."
      Dim split As String() = Nothing

      Console.WriteLine("The delimiters are -{0}-", delimStr)
      Dim x As Integer
      For x = 1 To 5
         split = words.Split(delimiter, x)
         Console.WriteLine(ControlChars.Cr + "count = {0,2} ..............", x)
         Dim s As String
         For Each s In  split
            Console.WriteLine("-{0}-", s)
         Next s
      Next x
   End Sub 
End Class 
' The example displays the following output:
'       The delimiters are - ,.:-
'       count =  1 ..............
'       -one two,three:four.-
'       count =  2 ..............
'       -one-
'       -two,three:four.-
'       count =  3 ..............
'       -one-
'       -two-
'       -three:four.-
'       count =  4 ..............
'       -one-
'       -two-
'       -three-
'       -four.-
'       count =  5 ..............
'       -one-
'       -two-
'       -three-
'       -four-
'       --

C#
using System;

public class StringSplit2 
{
   public static void Main() 
   {
      string delimStr = " ,.:";
       char [] delimiter = delimStr.ToCharArray();
      string words = "one two,three:four.";
      string [] split = null;

       Console.WriteLine("The delimiters are -{0}-", delimStr);
       for (int x = 1; x <= 5; x++) 
      {
          split = words.Split(delimiter, x);
         Console.WriteLine("\ncount = {0,2} ..............", x);
           foreach (string s in split) 
          {
             Console.WriteLine("-{0}-", s);
             }
       }
   }
}
// The example displays the following output:
//       The delimiters are - ,.:-
//       count =  1 ..............
//       -one two,three:four.-
//       count =  2 ..............
//       -one-
//       -two,three:four.-
//       count =  3 ..............
//       -one-
//       -two-
//       -three:four.-
//       count =  4 ..............
//       -one-
//       -two-
//       -three-
//       -four.-
//       count =  5 ..............
//       -one-
//       -two-
//       -three-
//       -four-
//       --

Visual C++
using namespace System;
using namespace System::Collections;
int main()
{
   String^ delimStr = " ,.:";
   array<Char>^delimiter = delimStr->ToCharArray();
   String^ words = "one two,three:four.";
   array<String^>^split = nullptr;
   Console::WriteLine( "The delimiters are -{0}-", delimStr );
   for ( int x = 1; x <= 5; x++ )
   {
      split = words->Split( delimiter, x );
      Console::WriteLine( "\ncount = {0, 2} ..............", x );
      IEnumerator^ myEnum = split->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         String^ s = safe_cast<String^>(myEnum->Current);
         Console::WriteLine( "-{0}-", s );
      }

   }
}
// The example displays the following output:
//       The delimiters are - ,.:-
//       count =  1 ..............
//       -one two,three:four.-
//       count =  2 ..............
//       -one-
//       -two,three:four.-
//       count =  3 ..............
//       -one-
//       -two-
//       -three:four.-
//       count =  4 ..............
//       -one-
//       -two-
//       -three-
//       -four.-
//       count =  5 ..............
//       -one-
//       -two-
//       -three-
//       -four-
//       --

J#
import System.*;

public class StringSplit2
{
    public static void main(String[] args)
    {
        String delimStr = " ,.:";
        char delimiter[] = delimStr.ToCharArray();
        String words = "one two,three:four.";
        String split[] = null;

        Console.WriteLine("The delimiters are -{0}-", delimStr);
        for (int x = 1; x <= 5; x++) {
            split = words.Split(delimiter, x);
            Console.WriteLine("\ncount = {0,2} ..............",
                System.Convert.ToString(x));
            for (int iCtr = 0; iCtr < split.get_Length(); iCtr++) {
                String s = split[iCtr];
                Console.WriteLine("-{0}-", s);
            }
        }
    } //main
} //StringSplit2
// The example displays the following output:
//      The delimiters are - ,.:-
//      count =  1 ..............
//      -one two,three:four.-
//      count =  2 ..............
//      -one-
//      -two,three:four.-
//      count =  3 ..............
//      -one-
//      -two-
//      -three:four.-
//      count =  4 ..............
//      -one-
//      -two-
//      -three-
//      -four.-
//      count =  5 ..............
//      -one-
//      -two-
//      -three-
//      -four-
//      --

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker