This topic has not yet been rated - Rate this topic

String.ToCharArray Method

April 12, 2014

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public char[] ToCharArray()

Return Value

Type: 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 code example demonstrates how to easily create a Unicode character array from a String. The array is then used with the Split method.


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'
//       ''    


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.