Greetings,

I have an array that stores words and their equivalent phonetically sounding codes as strings like this:

1:abat
12:abash
134:abridge

What I want to do is split each string and store the code only, so basically I want to store everything prior to the ":" .

I've wrote the following code

foreach (string intvalue in total)
             {
                 string[] storage =intvalue.Split(':');
             }

But this stores the code and the corresponding word underneath on the next line. I want the program to ignore everything after and including the ":", but I just cant find a way to articulate my needs. Any help is greatly appreciated, thanks.

Recommended Answers

All 2 Replies

All you have to do is take the value of storage[0] once you've split the current inttotal and do whatever you need with it. Keep in mind that storage[] goes out of scope once you iterate within the foreach loop, so handle the value however you need to within the confines of that loop. If you need these values after the loop is finished, consider populating a List<string> with the values.

List<string> permStorage = new List<string>();
             foreach (string intvalue in total)
             {
                 string[] storage =intvalue.Split(':');
                 permStorage.Add(storage[0]);
             }

Just what I wanted, thanks a lot .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.