C# ToCharArray Method, Converting String to Array

by Sam Allen - Updated January 7, 2010
Letters of the type name char

You require a character array buffer from a string in your C# program. The ToCharArray method is excellent for this purpose as it returns a new char[] array, which you can manipulate in-place like a normal array instead of a string. Here we look at the ToCharArray method in depth, first seeing an example program in the C# language, and then reviewing practical uses and its internal implementation.

Using ToCharArray method

Here we look at an example of using ToCharArray to receive a character array from the contents of a string. The example uses an input string, and then assigns a char[] array to the result of the parameterless instance method ToCharArray(). Next, it uses a for loop to get each character in the array, finally writing it to the Console.

=== Program that uses ToCharArray (C#) ===

using System;

class Program
{
    static void Main()
    {
        //
        // Input string.
        //
        string value = "Dot Net Perls";

        //
        // Use ToCharArray to convert string to array.
        //
        char[] array = value.ToCharArray();

        //
        // Loop through array.
        //
        for (int i = 0; i < array.Length; i++)
        {
            //
            // Get character from array.
            //
            char letter = array[i];
            //
            // Display each letter.
            //
            Console.Write("Letter: ");
            Console.WriteLine(letter);
        }
    }
}

=== Output of the program ===

Letter: D
Letter: o
Letter: t
Letter:  
Letter: N
Letter: e
Letter: t
Letter:  
Letter: P
Letter: e
Letter: r
Letter: l
Letter: s

Uses

The core benefit to using ToCharArray is that the char[] array you receive is mutable, meaning you can actually change each individual character. For this reason, the method is ideal when you need to transform characters, as in ROT13, or uppercasing the first letter.

(See Uppercase First Letter.)

(See Reverse String.)

(See ROT13.)

(See Char Lookup Table.)

(See Whitespace String Methods for Newlines, Tabs and Spaces.)

(See Randomize Chars in String.)

Converting char arrays to strings. Often you will need to change your char array you received from ToCharArray back to a string, so it can be used easily in the rest of your program. You can use the new string constructor for this.

(See Convert Char Array to String.)

Internal implementation

You can see that the ToCharArray instance method uses unsafe code that manipulates pointers, along with the private wstrcpyPtrAligned method in the base class library. For this reason, it is normally faster than doing the same thing in managed code, partly because managed code has to check array bounds. However, it makes a complete pass over the string, so if you do not require that, filling character arrays manually may be faster.

(See Char Array Use.)

Summary

Here we looked at an example of using the parameterless ToCharArray method on the string type in the C# language. This method returns a character array, which you can modify in-place, sometimes improving performance and simplicity of your code. Finally, we looked at practical uses of ToCharArray and its internals in the base class library.

(Do not copy this page.)

Dot Net Perls | Search
Arrays | 2D Array Use | Char Array Use | Convert List to Array | Count Array Elements | String Array
C# | Obsolete Attribute | True and False | Gradient Tips | Catch Examples
© 2010 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen