String.ToCharArray Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

Syntax

Public Function ToCharArray As Char()
public char[] ToCharArray()

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.

Examples

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

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim delimStr As String = " ,.:"
      Dim delimiter() As Char = delimStr.ToCharArray()
      Dim words As String = "one two,three:four."
      Dim split() As String = Nothing

      outputBlock.Text &= "The delimiters are:" & vbCrLf
      For Each ch As Char In delimStr
         outputBlock.Text &= String.Format("   '{0}'", ch) + vbCrLf
      Next
      outputBlock.Text &= vbCrLf

      split = words.Split(delimiter)
      For Each s As String In split
         outputBlock.Text += String.Format("'{0}'", s) & vbCrLf
      Next 
   End Sub 
End Class 
' The example displays the following output:
'       
'       The delimiters are:
'          ''
'          ','
'          '.'
'          ':'
'          
'       'one'
'       'two'
'       'three'
'       'four'
'       ''    
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string delimStr = " ,.:";
      char[] delimiter = delimStr.ToCharArray();
      string words = "one two,three:four.";
      string[] split = null;

      outputBlock.Text += "The delimiters are:" + "\n";
      foreach (char ch in delimStr)
         outputBlock.Text += String.Format("   '{0}'", ch) + "\n";

      outputBlock.Text += "\n";

      split = words.Split(delimiter);
      foreach (string s in split)
      {
         outputBlock.Text += String.Format("'{0}'", s) + "\n";
      }
   }
}
// The example displays the following output:
//       
//       The delimiters are:
//          ''
//          ','
//          '.'
//          ':'
//          
//       'one'
//       'two'
//       'three'
//       'four'
//       ''    

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

String Class

ToCharArray Overload

System Namespace

Char

Array