Given a string, the task is to convert this string into a character array in C#.
Examples:
Input: string Output: [s, t, r, i, n, g] Input: GeeksForGeeks Output: [G, e, e, k, s, F, o, r, G, e, e, k, s]
Method 1: Naive Approach
Step 1: Get the string.
Step 2: Create a character array of the same length as of string.
Step 3: Traverse over the string to copy character at the i’th index of string to i’th index in the array.
Step 4: Return or perform the operation on the character array.
Below is the implementation of the above approach:
C#
// C# program to Convert a string // to a Character array // using Naive Approach using System; public class GFG{ static public void Main () { string str = "GeeksForGeeks"; // Creating array of string length char[] ch = new char[str.Length]; // Copy character by character into array for (int i = 0; i < str.Length; i++) { ch[i] = str[i]; } // Printing content of array foreach (char c in ch) { Console.WriteLine(c); } } } |
Output:
G e e k s F o r G e e k s
Method 2: Using toCharArray() Method
Step 1: Get the string.
Step 2: Create a character array of same length as of string.
Step 3: Store the array return by toCharArray() method.
Step 4: Return or perform operation on character array.
C#
// C# program to Convert a string // to a Character array // using ToCharArray method using System; public class GFG{ static public void Main () { string str = "GeeksForGeeks"; // Creating array of string length // Copy character by character into array char[] ch = str.ToCharArray(); // Printing content of array foreach (char c in ch) { Console.WriteLine(c); } } } |
Output:
G e e k s F o r G e e k s
Recommended Posts:
- Convert a Character to the String in C#
- Different Ways to Convert Char Array to String in C#
- Different ways to convert String to Integer in C#
- C# | Convert.FromBase64String(String) Method
- C# | Convert.ToUInt16(String, IFormatProvider) Method
- C# | Convert.ToInt64(String, IFormatProvider) Method
- C# | Convert.ToSByte(String, IFormatProvider) Method
- C# | Convert.ToInt16(String, IFormatProvider) Method
- C# | Convert.ToUInt32(String, IFormatProvider) Method
- C# | Convert.ToUInt64(String, IFormatProvider) Method
- C# | Convert.ToDouble(String, IFormatProvider) Method
- C# | Convert.ToSingle(String, IFormatProvider) Method
- C# | Convert.ToString(String, IFormatProvider) Method
- C# Program to Convert a Binary String to an Integer
- C# | Convert.ToByte(String, IFormatProvider) Method
- C# | Convert.ToDecimal(String, IFormatProvider) Method
- C# | Convert.ToDateTime(String, IFormatProvider) Method
- C# | Convert.ToChar(String, IFormatProvider) Method
- C# | Convert.ToBoolean(String, IFormatProvider) Method
- C# | Convert.ToInt32(String, IFormatProvider) Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

