Solved

using Split() Method to separate out data into Array

Posted on 2009-04-06
3
522 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
ID: 24082992
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
ID: 24083040
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
ID: 24083203
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Name space syntax error 12 56
C# HTTP GET method sample code 3 57
RLDC Reporting in Visual studio 11 13
VB.NET 2008 - SQL Timeout 9 23
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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