c# string to array code example

Example 1: c# array

int[] array = new int[]{1, 2, 3, 4, 5};
foreach (string i in array) 
{
  Console.WriteLine(i);
}

//or

for(int i=0; i<array.Length; i++){
	  Console.WriteLine(array[i]);
}

Example 2: c# string to character array

string chars = "Array";
char[] letters = chars.toCharArray();

Example 3: c# string array to string

string[] test = new string[2];

test[0] = "Hello ";
test[1] = "World!";

string.Join("", test);

Example 4: making a list of chars in c#

string data = "ABCDEFGHIJ1fFJKAL";
List<char> datalist = new List<char>();
datalist.AddRange(data);

Example 5: convert string to array c#

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

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

Example 6: 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'};