user3558247
user3558247

Reputation: 13

C# create array from another array

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"

I can then get:

SplitEC[0]

e.g. asdf

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

e.g. SplitEC[2] would be d

Upvotes: 0

Views: 438

Answers (2)

p.s.w.g
p.s.w.g

Reputation: 149060

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

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

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

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

Or if you want an array of chars, you can call 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:

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

Upvotes: 4

Simon Whitehead
Simon Whitehead

Reputation: 65087

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

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

Upvotes: 3

Related Questions