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.
Similar Reads
How to Convert ASCII Char to Byte in C#? In C#, converting an ASCII character to a byte is a common task, particularly useful in data encoding and manipulation. In this article, we will learn how to Convert ASCII Char to Byte in C#.ExampleInput: 'G' Output: 71 Explanation: The ASCII character 'G' corresponds to the integer value 71, which
2 min read
How to Get a Comma Separated String From an Array in C#? Given an array, now our task is to get a comma-separated string from the given array. So we can do this task using String.Join() method. This method concatenates the items of an array with the help of a comma separator between each item of the array. Syntax: String.Join(",", array_name)Where array_n
2 min read
C# Program to Convert the Octal String to an Integer Number Given an octal number as input, we need to write a program to convert the given octal number into equivalent integer. To convert an octal string to an integer, we have to use Convert.ToInt32() function to convert the values. Examples: Input : 202 Output : 130 Input : 660 Output : 432 Convert the ite
2 min read
C# | Copying the entire ArrayList to 1-D Array starting at the specified index ArrayList.CopyTo(Array, Int32) Method is used to copy the entire ArrayList to a compatible one-dimensional Array, starting at the specified index of the target array. Syntax: public virtual void CopyTo (Array array, int arrayIndex); Parameters: array: It is the one-dimensional Array that is the dest
4 min read
C# Program for Converting Hexadecimal String to Integer Given an hexadecimal number as input, we need to write a program to convert the given hexadecimal number into equivalent integer. To convert an hexadecimal string to integer, we have to use Convert.ToInt32() function to convert the values. Syntax: Convert.ToInt32(input_string, Input_base); Here, inp
2 min read
Convert a Character to the String in C# Given a character, the task is to character to the string in C#. Examples: Input : X = 'a' Output : string S = "a" Input : X = 'A' Output : string S = "A" Approach: The idea is to use ToString() method, the argument is the character and it returns string converting Unicode character to the string. /
1 min read