Convert String to Character Array in C#
Last Updated :
15 Jul, 2025
In C#, converting a string to a character array is a straightforward process that allows you to manipulate individual characters within the string. This conversion can be useful for various operations, such as character manipulation, processing text, or analyzing strings.
Example
Input: "Hello, Geeks!";
Output: H, e, l, l, o, , , , G, e, e, k, s, !
Explanation: The string "Hello, Geeks!" is converted into a character array, resulting in the individual characters being displayed, including spaces and punctuation.
Syntax
char[] charArray = new char[input.Length];
for (int i = 0; i < input.Length; i++) { # Naive Method
charArray[i] = input[i];
}
char[] charArray = stringVariable.ToCharArray(); # ToCharArray() Method
char[] charArray = input.Select(c => c).ToArray(); # LINQ Method Naive Method
The naive method involves manually creating a character array and populating it with each character of the string.
Syntax
char[] charArray = new char[input.Length];
for (int i = 0; i < input.Length; i++) {
charArray[i] = input[i];
}
C#
using System;
public class GFG {
static public void Main() {
string input = "Hello, Geeks!";
char[] charArray = new char[input.Length];
// Manually populating the character array
for (int i = 0; i < input.Length; i++) {
charArray[i] = input[i];
}
Console.WriteLine(string.Join(" , ", charArray));
}
}
OutputH , e , l , l , o , , , , G , e , e , k , s , !
Using ToCharArray() Method
The ToCharArray() method converts a string into a character array.
Syntax
char[] charArray = stringVariable.ToCharArray();
Example
C#
using System;
public class GFG {
static public void Main() {
string input = "Hello, Geeks!";
char[] charArray = input.ToCharArray();
Console.WriteLine("Character Array: " + string.Join(" , ", charArray));
}
}
OutputCharacter Array: H , e , l , l , o , , , , G , e , e , k , s , !
Using LINQ Method
This method utilizes the LINQ library to convert a string into a character array.
Syntax
char[] charArray = input.Select(c => c).ToArray();
Example
C#
using System;
using System.Linq;
public class GFG {
static public void Main() {
string input = "Hello, Geeks!";
char[] charArray = input.Select(c => c).ToArray();
Console.WriteLine("Character Array: " + string.Join(" , ", charArray));
}
}
OutputCharacter Array: H , e , l , l , o , , , , G , e , e , k , s , !
Conclusion
Converting a string to a character array in C# is essential for various string manipulations and analyses. The methods presented above provide effective ways to achieve this conversion, each serving different use cases. The naive method offers a straightforward approach, while the ToCharArray(), LINQ provide efficient alternatives for converting strings to character arrays.
Explore
Introduction
Fundamentals
Control Statements
OOP Concepts
Methods
Arrays
ArrayList
String
Tuple
Indexers