2 column csv file to 2 dim array
Im new to C# and was wondering if there was a more elegant way to do the following:
thanks in advance for any help!
VB Code:
if (ofd1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader objFile = new System.IO.StreamReader(ofd1.FileName);
string[] arrFirst = objFile.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
objFile.Close();
objFile.Dispose();
//int intUB = arrFirst.GetUpperBound(0);
string[,] arrSecond;
arrSecond = new string[ arrFirst.GetUpperBound(0),2];
string[] arrTemp;
arrTemp = new string[1];
for (int intCounter = 0; intCounter <= arrFirst.GetUpperBound(0) - 1; intCounter++)
{
arrTemp = arrFirst[intCounter].Split(',');
arrSecond[ intCounter,0] = arrTemp[0];
arrSecond[intCounter,1] = arrTemp[1];
}
}
Re: 2 column csv file to 2 dim array
As I mentioned in another thread you posted to, your use of Split is incorrect. The overload you're using will split on evry occurrence of any character in the array you specify, which means that it will split on every '\r' and every '\n' and add an empty entry for the zero length string between each pair. The RemoveEmptyEntries will then remove them so your end result is the same, but why put them in only to remove them? You want to split on every line break, and a line break is the "\r\n" substring, so that's what you need to specify:
Code:
string[] arrFirst = objFile.ReadToEnd().Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
That will split on every occurrence of any substring in the array, and "\r\n" is the only one, so it will split on the line breaks.
Re: 2 column csv file to 2 dim array
Quote:
Originally Posted by jmcilhinney
As I mentioned in another thread you posted to, your use of Split is incorrect. The overload you're using will split on evry occurrence of any character in the array you specify, which means that it will split on every '\r' and every '\n' and add an empty entry for the zero length string between each pair. The RemoveEmptyEntries will then remove them so your end result is the same, but why put them in only to remove them? You want to split on every line break, and a line break is the "\r\n" substring, so that's what you need to specify:
Code:
string[] arrFirst = objFile.ReadToEnd().Split(new string[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
That will split on every occurrence of any substring in the array, and "\r\n" is the only one, so it will split on the line breaks.
not sure if you saw my other reply, but the code works fine for me as i posted it. I cant seem to get yours to work though. Please help me see what I am missing.
The whole thing the way i wrote it does seem kinda clunky to me, though.
and thanks for the help!
Re: 2 column csv file to 2 dim array
As I said, your code will produce the correct end result but it goes about getting that result in an unnecessarily roundabout way. Test this code:
Code:
string str = "Line 1\r\nLine 2\r\nLine 3";
string[] splitWithCharArray = str.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.None);
string[] splitWithStringArray = str.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
MessageBox.Show(string.Join(Environment.NewLine, splitWithCharArray));
MessageBox.Show(string.Join(Environment.NewLine, splitWithStringArray));
and you'll see how using ToCharArray creates more elements than are required. That's because your method splits the original string on every '\r' and also on every '\n', while my method splits it only on every "\r\n", so your method splits at twice as many points. By specifying RemoveEmptyEntries you are telling Split to remove those additional empty entries, but they are still created to begin with. As I said, why put them in only to remove them when you don't have to put them in in the first place?
Re: 2 column csv file to 2 dim array
that does work! I was really more worried about the other areas of the snippet though. I just kinda feel like im going the long way about getting the data into a 2 dim array.
Thanks for the help!
Re: 2 column csv file to 2 dim array
You're going to have to use a loop somewhere along the line. Assuming that your file is known to conform to the correct format, this is about as good as you're going to get:
Code:
string[] lines = System.IO.File.ReadAllLines(ofd.FileName);
string[,] matrix = new string[lines.Length, 2];
string[] lineParts;
for (int i = 0; i < lines.Length; i++)
{
lineParts = lines[i].Split(',');
matrix[i, 0] = lineParts[0];
matrix[i, 1] = lineParts[1];
}
If you don't know for sure that the file conforms to the correct format then I'd advise this:
Code:
string[] lines = System.IO.File.ReadAllText(ofd.FileName).Split(new string[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries);
string[,] matrix = new string[lines.Length, 2];
string[] lineParts;
for (int i = 0; i < lines.Length; i++)
{
lineParts = lines[i].Split(',');
matrix[i, 0] = lineParts[0].Trim();
if (lineParts.Length > 1)
{
matrix[i, 1] = lineParts[1].Trim();
}
}