split a string and loop through array

Posted on 2007-07-27
Last Modified: 2010-04-15
hi, i have a string like this: w1;w2;s3;s4.

I want to split this string and store each item in an array and then be able to loop through the array.  How would I do this?
Question by:mitesh114
    LVL 142

    Accepted Solution

    string data = "w1;w2;s3;s4";
    string[] c = data.Split(';');
    foreach ( string i in c )
    {  // handle i
    LVL 20

    Expert Comment

    personally I would use

    string[] c = System.Text.RegularExpressions.Regex.Split(";","YourInput");

    as it it faster than the string Split method.

    Andrew :-)
    LVL 9

    Expert Comment

    > as it it faster than the string Split

    do you have a reference that demonstrates that Regex.Split is faster - the sources I have seen say that Regex.Split is significantly *slower* than string.split (even with a compiled regex) - I guess because the functionality behind it is much more complex...
    LVL 20

    Expert Comment

    I stand corrected lol

    My evidence came from a past project I did. But you have prompted me to look at the theory nehind it, and as I say I stand corrected.




    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now