0

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

2 Answers 2

4

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"
Sign up to request clarification or add additional context in comments.

3 Comments

Why the downvote? If there is anything incorrect or unclear in my answer I'd glad to correct.
Unfortunate.. your answer has more detail and you also received a downvote. +1 to undo the stupid downvote.
@SimonWhitehead +1 to you too. I think you beat me by a few seconds anyway. :)
3

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.