Solved

using Split() Method to separate out data into Array

Posted on 2009-04-06
3
521 Views
Last Modified: 2013-12-17
I have a Console application that gets a filename passed as an argument. It then stores that filenames data to an array, gets opened and then read line by line and stored to another array.
I'm currently using the FileStream ReadLine() method to pull out the workstations one by one and store to this Array. The escape sequence is \r\n. However, if I receive a text file whereby the data has delimiters like a semi-colon (;) or commas (,) or whatever, I would like to change the code to allow me to handle these. What other class can I use to Open this file and retrieve and store data to array while handling / splitting out the escape sequences properly?
Thanks Experts,
Wally
static void Main(string[] Args)

        {

            // Do Database Query Here.....

            // This will hold the entire workstation 

            // list that is pulled from the Nodes table

            List<Program> nodeList = new List<Program>();

            string idx = "";

            if (Args.Length >= 1)

            {

                for (int i = 0; i < Args.Length; i++)

                {

                    idx = Args[i];

                    //Console.WriteLine(idx);

                }

            }
 

            string filename = "";

                filename = Args[0];

                filename = filename.ToUpper();

                //Console.WriteLine(clArgs[0]);

                if (File.Exists(filename))

                {

                    FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

                    using (StreamReader sr = new StreamReader(fs))

                    {

                        string line;

                        while ((line = sr.ReadLine()) != null)

                        {

                            Program node = new Program();

                            node.NodeToPing = line;

                            nodeList.Add(node);

                        }

                    }

                }

Open in new window

0
Comment
Question by:wally_davis
3 Comments
 
LVL 5

Expert Comment

by:mrcoolcoder
Comment Utility
What I do when I want to allow multiple escape characters is I use replace.

So "," will become ";"
And ":" will become ";"
And So on...

Then you just run your split function.
0
 

Author Comment

by:wally_davis
Comment Utility
What I'm looking for is some help on how to modify the code above since there is no .Replace() Method within the StreamReader.
0
 
LVL 18

Accepted Solution

by:
philipjonathan earned 500 total points
Comment Utility
Try changing lines 24-33 to:

using (StreamReader sr = new StreamReader(fs))

{

  string data = sr.ReadToEnd();

  string[] nodeDatas = data.Split(new char[] {',', ';', '\r', '\n'});

  foreach (string line in nodeDatas)

  {

    if (!nodeData.Trim().Equals(string.Empty))

    {

      Program node = new Program();

      node.NodeToPing = line;

      nodeList.Add(node);

    }

  }

}

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

8 Experts available now in Live!

Get 1:1 Help Now