In C#, ToCharArray() copies the characters of a string into a new char[]. Each element of the array is one character, in the same order.
The array is a separate copy. You can change its elements, sort or reverse them, and then build a new string with new string(array). The original string never changes, because strings are immutable but arrays are not. To read characters without copying, simply use the indexer, text[i], or a foreach loop.
This is an instance member of System.String. Call it on a string variable, such as text.ToCharArray(...). Calling it on a null variable throws NullReferenceException. It copies characters to a separate place. Strings are immutable, so changing that copy never changes the original string.
Syntax
Call it with no arguments for all characters, or give a start and a length.
C#
string word = "hello";
char[] all = word.ToCharArray(); // { 'h', 'e', 'l', 'l', 'o' }
char[] part = word.ToCharArray(1, 3); // { 'e', 'l', 'l' }
string back = new string(all); // "hello"
Call
What it does
text.ToCharArray()
Copy every character into a new array.
text.ToCharArray(6, 5)
Copy 5 characters starting at index 6.
new string(chars)
Turn an edited array back into a string.
startIndex and length must stay inside the string, or you get ArgumentOutOfRangeException. An empty string gives an empty array (length 0). For copying into an array you already have, see CopyTo().
Library declarations
C#
public char[] ToCharArray();
public char[] ToCharArray(int startIndex, int length);
Parameters
Parameter
Description
startIndex
Optional. The first character to copy.
length
Optional. How many characters to copy.
Return Value
Return Value
Description
char[]
A new array holding a copy of the characters.
C# Examples
Example 1 — Turn a Word into Characters
Copy a word into an array, then read its length and a few elements.
C#
using System;
class Program {
static void Main() {
char[] letters = "hello".ToCharArray();
Console.WriteLine(letters.Length);
Console.WriteLine(letters[0]);
Console.WriteLine(letters[4]);
}
}
Output
5
h
o
How It Works
One element per character."hello" has five characters, so the array has five elements.
Same order. Index 0 holds 'h', just like in the string.
The last element. Index 4 (length minus one) holds 'o'.
A real array.letters is an ordinary char[]; you can pass it to any method that takes an array.
Example 2 — Reverse a String
Strings cannot be reversed in place, but an array can. Copy, reverse, and rebuild.
C#
using System;
class Program {
static void Main() {
string word = "stressed";
char[] arr = word.ToCharArray();
Array.Reverse(arr);
string reversed = new string(arr);
Console.WriteLine(reversed);
Console.WriteLine(word);
}
}
Output
desserts
stressed
How It Works
Copy into an array.ToCharArray() gives a version of the word that can be changed.
Reverse in place.Array.Reverse(arr) swaps the elements so the last character comes first.
Back to text.new string(arr) creates desserts from the reversed characters.
Two separate strings.word still reads stressed; the array was a copy.
Example 3 — Change One Letter and Copy a Range
Swap the first letter of a word through the array, then copy just one word out of a phrase.
C#
using System;
class Program {
static void Main() {
string word = "cat";
char[] chars = word.ToCharArray();
chars[0] = 'b';
Console.WriteLine(word);
Console.WriteLine(new string(chars));
char[] part = "Hello World".ToCharArray(6, 5);
Console.WriteLine(new string(part));
}
}
Output
cat
bat
World
How It Works
Arrays can change.chars[0] = 'b' is allowed, while word[0] = 'b' would not even compile.
The string ignores it.word still prints cat, because the array is an independent copy.
Rebuild to see the change.new string(chars) turns the edited array into bat.
Copy a slice.ToCharArray(6, 5) copies five characters from index 6, which spell World.
Frequently Asked Questions
It returns a new char array with a copy of the string's characters, in the same order.
No. The array is a copy. Strings are immutable, so you need new string(array) to get the changed text.
Use new string(chars). For part of the array, use new string(chars, startIndex, length).
Copy it with ToCharArray(), call Array.Reverse on the array, and create a new string from it. Note that this reverses UTF-16 chars, which can split emoji.
No. You can use foreach (char c in text) or text[i] directly. Use ToCharArray when you need a separate array you can change.