How to split a sentence into letters and save as an array of strings in C #?

I found this on the Internet:

char[] characters = "this is a test".ToCharArray();

but this array is in the array char. I would like to be able to store it in an array Stringbecause of the function that I use after it only works with String.

Or is there another way to put all the letters in an array, and then select the index of the array, where the value matches the letter from the array.

Like this:

String[] alphabet = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", " "};

Now the first letter char[]will be "t".

Now I need an index: 19, which will be stored in the list.

+3
source share
1 answer
string[] arr = "this is a test".Select(c => c.ToString()).ToArray();

to save indices you don't need this array alphabet

int[] indexes = "this is a test".Select(c => (int)(c-'a')).ToArray();

You can see the output in LinqPad

enter image description here

, , , IndexOf n -

List<char> alphabet = "abcdefghijklmnopqrstuvwxyz  ".ToList();
int[] arr = "this is a test".Select(c => alphabet.IndexOf(c)).ToArray();
+6

All Articles