StringToCharArray Method
 

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

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

Syntax
public char[] ToCharArray()
public:
array<wchar_t>^ ToCharArray()
member ToCharArray : unit -> char[]
Public Function ToCharArray As Char()

Return Value

Type:

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.

Remarks

This method copies each character (that is, each Char object) in a string to a character array. The first character copied is at index zero of the returned character array; the last character copied is at index – 1.

To create a string from the characters in a character array, call the String constructor.

To create a byte array that contains the encoded characters in a string, instantiate the appropriate Encoding object and call its EncodingGetBytes 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.

Examples

The following example calls the ToCharArray method to extract the characters in a string to a character array. It then displays the original string and the elements in the array.

using System;

public class Example
{
   public static void Main()
   {
      String s = "AaBbCcDd";
      var chars = s.ToCharArray();
      Console.WriteLine("Original string: {0}", s);
      Console.WriteLine("Character array:");
      for (int ctr = 0; ctr < chars.Length; ctr++)
         Console.WriteLine("   {0}: {1}", ctr, chars[ctr]);
   }
}

// The example displays the following output:
//     Original string: AaBbCcDd
//     Character array:
//        0: A
//        1: a
//        2: B
//        3: b
//        4: C
//        5: c
//        6: D
//        7: d
Module Example
   Public Sub Main()
      Dim s As String = "AaBbCcDd"
      Dim chars() = s.ToCharArray()
      Console.WriteLine("Original string: {0}", s)
      Console.WriteLine("Character array:")
      For ctr = 0 to chars.Length - 1
         Console.WriteLine("   {0}: {1}", ctr, chars(ctr))
      Next
   End Sub
End Module
' The example displays the following output:
'     Original string: AaBbCcDd
'     Character array:
'        0: A
'        1: a
'        2: B
'        3: b
'        4: C
'        5: c
'        6: D
'        7: d

The following example defines a string containing the characters that serve as delimiters in a delimited string. It then calls the ToCharArray method to create a character array that can be passed to the Split method to separate the delimited string into its individual substrings.


  

  
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-
'       --
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-
//       --
Version Information
Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top