简体   繁体   English

如何将一个句子分成字母并在C#中存储为一个String数组?

[英]how to split a sentence into letters and store as a String array in C#?

I found this on the Internet: 我在互联网上发现了这个:

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

but this stores is into a char array. 但这个商店是一个char数组。 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 . 我希望能够将它存储到一个String数组中,因为我使用的函数只能用于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" . 现在char[]的第一个字母将是"t"

Now what I need is the index: 19 to be stored in a list. 现在我需要的是索引:19存储在列表中。

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

to store the indexes you don't need this alphabet array 存储您不需要此alphabet数组的索引

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

You can see the output in LinqPad 您可以在LinqPad中看到输出

在此输入图像描述

This is the way, most similar to your question, but I don't think using IndexOf n times is a good solution 这是与您的问题最相似的方式,但我不认为使用IndexOf n次是一个很好的解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM