C#从另一个数组创建数组

时间:2021-08-24 16:03:01

Hopefully its a really simple solution to this. I've got a string that I've split by "-" which I then want to split into another array but can't seem to get it to work. Any help appreciated.

希望它是一个非常简单的解决方案。我有一个字符串,我用“ - ”拆分,然后我想分成另一个数组,但似乎无法让它工作。任何帮助赞赏。

SplitEC = textBox1.Text.Split('-');

e.g. textBox1.text = "asdf-asfr"

例如textBox1.text =“asdf-asfr”

I can then get:

然后我可以得到:

SplitEC[0]

e.g. asdf

例如ASDF

I then want to get each individual element of SplitEC[0] but for the life of me nothing works.

然后我想得到SplitEC [0]的每个元素,但是对于我的生活没有任何作用。

e.g. SplitEC[2] would be d

例如SplitEC [2]将是d

2 个解决方案

#1


3  

You can simply chain the array indexers. SplitEC[0] returns a string.. which implements the indexer for individual chars..

您可以简单地链接数组索引器。 SplitEC [0]返回一个字符串..它实现了各个字符的索引器。

char c = SplitEc[0][2]; // d
//       |________||__|
//        ^ string  ^  characters of the string

#2


4  

Because SplitEC[0] is a string, you could just access the characters individually like this:

因为SplitEC [0]是一个字符串,你可以像这样单独访问这些字符:

char c = SplitEC[0][2];                // 'd'

Note that the result is a char; if you want a string just call ToString():

注意结果是char;如果你想要一个字符串只需调用ToString():

string c = SplitEC[0][2].ToString();   // "d"

Or if you want an array of chars, you can call ToCharArray:

或者如果你想要一个字符数组,你可以调用ToCharArray:

char[] chars = SplitEC[0].ToCharArray();
char c = char[2];                      // 'd'

Or if you want an array of strings, you can use a little linq:

或者如果你想要一个字符串数组,你可以使用一点linq:

string[] charStrings = SplitEC[0].Select(Char.ToString).ToArray();
string c = charStrings[2];             // "d"

#1


3  

You can simply chain the array indexers. SplitEC[0] returns a string.. which implements the indexer for individual chars..

您可以简单地链接数组索引器。 SplitEC [0]返回一个字符串..它实现了各个字符的索引器。

char c = SplitEc[0][2]; // d
//       |________||__|
//        ^ string  ^  characters of the string

#2


4  

Because SplitEC[0] is a string, you could just access the characters individually like this:

因为SplitEC [0]是一个字符串,你可以像这样单独访问这些字符:

char c = SplitEC[0][2];                // 'd'

Note that the result is a char; if you want a string just call ToString():

注意结果是char;如果你想要一个字符串只需调用ToString():

string c = SplitEC[0][2].ToString();   // "d"

Or if you want an array of chars, you can call ToCharArray:

或者如果你想要一个字符数组,你可以调用ToCharArray:

char[] chars = SplitEC[0].ToCharArray();
char c = char[2];                      // 'd'

Or if you want an array of strings, you can use a little linq:

或者如果你想要一个字符串数组,你可以使用一点linq:

string[] charStrings = SplitEC[0].Select(Char.ToString).ToArray();
string c = charStrings[2];             // "d"