[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

using Split() Method to separate out data into Array

Posted on 2009-04-06
3
Medium Priority
?
528 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 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