Вопрос

I found this on the Internet:

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

but this stores is into a char array. I would like to be able to store it into a String array because of the function I use after it only works with a String.

Or is there another way to put all the letters in an array and then select the index of an array where the value is the same as the letter out of 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 of the char[] will be "t".

Now what I need is the index: 19 to be stored in a list.

Это было полезно?

Решение

string[] arr = "this is a test".Select(c => c.ToString()).ToArray();

to store the indexes you don't need this alphabet array

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

You can see the output in LinqPad

enter image description here

This is the way, most similar to your question, but I don't think using IndexOf n times is a good solution

List<char> alphabet = "abcdefghijklmnopqrstuvwxyz  ".ToList();
int[] arr = "this is a test".Select(c => alphabet.IndexOf(c)).ToArray();
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top