convert string into array C# code example

Example 1: convert string to array c#

string myString = "foobar";
char[] myCharArray = myString.ToCharArray();

/* Example of myCharArray
{'f', 'o', 'o', 'b', 'a', 'r'}
*/

Example 2: create char array c#

char[] arr = new char[5];

arr[0] = 'a';
arr[1] = 'b';
arr[2] = 'c';
arr[3] = 'd';
arr[4] = 'e';

//OR
char[] arr = new char[] {'a', 'b', 'c', 'd', 'e'};

Example 3: how to turn a string in a char list c#

string sentence = "Mahesh Chand";  
char[] charArr = sentence.ToCharArray();  
foreach (char ch in charArr)  
{  
    Console.WriteLine(ch);  
}

Example 4: how to make a string a list of characters c#

string scentence = "Hi there"; // Defining a string to turn to characters

char[] charArr = scentence.ToCharArray() // Turns String to a list of characters
  
//The output of charArr would be:
//['H', 'i', ' ', 't', 'h', 'e', 'r', 'e']
  
  
/*
Answer by Ren Rawbone
*/