Windows Dev Center

Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

String.ToCharArray Method ()

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

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

public char[] ToCharArray ()
public char[] ToCharArray ()
public function ToCharArray () : char[]
Not applicable.

Return Value

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 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);
      	    }
	}
    }
}

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

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2015 Microsoft