Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more: (untagged)
Hey all,

I'm currently trying to create program which manipulates a comma delimited text file with a number of lines and each word surrounded by commas. The file is laid out like this:
"Name","ID","Number"
"Fencepost","414","5"
"Brick","234","7"


I would like to take each word of the file and save them as separate entries in a 2D array. I've been able to code this, the trouble I'm having is I also want to remove the quotation marks from around the entries. My code so far is:
C#
String fileName = null;
       String str = null;
       String[] strArr = null;
       String[][] allLines = null;
       String[][] noquotes = null;

       public void button1_Click(object sender, EventArgs e)
       {
           OpenFileDialog fdlg = new OpenFileDialog();
           fdlg.Title = "C# Corner Open File Dialog";
           fdlg.InitialDirectory = @"c:";
           fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
           fdlg.FilterIndex = 2;
           fdlg.RestoreDirectory = true;
           if (fdlg.ShowDialog() == DialogResult.OK)
           {
               fileName = fdlg.FileName;
           }
           str = File.ReadAllText(fileName, Encoding.UTF8);
           char[] splitchar = { '\n' };
           char[] splitArr = { ',' };
           strArr = str.Split(splitchar);
           allLines = new String[strArr.Length][];
           for (int i = 0; i < strArr.Length; i++)
           {
               allLines[i] = strArr[i].Split(splitArr);
            }
         }

Can anyone suggest what to add to this to remove all the " " from each allLines cell?
Posted
Updated 25-Oct-12 6:57am
v2
Comments
Sergey Alexandrovich Kryukov 25-Oct-12 13:25pm    
Do you need to remove all "", or only in certain position? What are the roles of all variables on top initialized to null? You rather need to use local variables as possible? So, where are the arrays? the opposite thing is the dialog -- having it local is bad, you are not re-using it.
--SA

If you want to delete all "", just use System.String.Replace(string, string):
C#
string myString; //...
//...
myString = myString.Replace(@"""", string.Empty); // "" with empty string

Please see:
http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx[^].

If you need to use some complex rule for removing quotation (which is often the case), learn and use Regular Expressions using the class System.Text.RegularExpressions.Regex:
http://en.wikipedia.org/wiki/Regular_expressions[^],
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx[^].

Please see my comment to the question. It's not clear how the question and your source code are related to the multidimensional arrays you declare. Probably, not related much.

Now, the bonus advice on reusing the dialog. Creating it from scratch all the time is bad, because you loose its position and all other aspects of its previous state. The most universal and robust approach is using lazy evaluation pattern in its most primitive form:
http://en.wikipedia.org/wiki/Lazy_evaluation[^].

Something like that:
C#
public partial class MyMainForm {

   OpenFileDialog openFileDialog; //don't use it directly; initialized to null

   OpenFileDialog OpenFileDialog { //use this one
      get {
          if (openFileDialog == null) { lazy initialization
              openFileDialog = new OpenFileDialog(); // constructed only once
              // do appropriate set up, only once; about "only once" -- please see below
          } //if
          return openFileDialog;
      }
   } //OpenFileDialog

} //class MyMainForm


This is much better then doing it all in the construction of declaring class. With Forms, this is less of a problem, but with WPF, the problem is that such setup could be performed too early, when general UI is not yet properly initialized, so the lazy pattern is the absolute winner in all cases.

For more fun, you can get familiar with much more general (and I admit, maybe too esoteric) "wish you were here… only once" pattern I put forward myself:
Wish You Were Here… Only Once[^]. :-)

—SA
 
Share this answer
 
v5
Comments
CPallini 25-Oct-12 15:10pm    
5.
Sergey Alexandrovich Kryukov 25-Oct-12 15:12pm    
Thank you, Carlo.
--SA
fjdiewornncalwe 25-Oct-12 15:23pm    
+5. Nice.
Sergey Alexandrovich Kryukov 25-Oct-12 15:24pm    
Thank you, Marcus.
--SA
Espen Harlinn 25-Oct-12 16:09pm    
5'ed!
You may remove quotation marks using String.Replace method, replacing occurrences of double quote with the empty string, e.g.
C#
string s = "\"name\"";
s = s.Replace("\"", string.Empty);



[changed "" with string.Empty, thanks to Sergey]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 25-Oct-12 13:45pm    
As simple as that, unless requirements are as simple, but they can be more difficult. My 5 anyway.
Just take a look at my answer -- I found more problems... :-)
--SA
Sergey Alexandrovich Kryukov 25-Oct-12 13:49pm    
Sorry, re-voted to 4 (a rare case) -- just for impurity of "" -- should better be string.Empty.

No, "" functions perfectly well (there was a heated discussion over the invalid use of "", but authors of .NET were not so stupid to allow multiple objects of identical values; they created string interning, so "" works correctly). It's just not so good for maintenance. I can explain why.

I hope you will understand my reason.
--SA
CPallini 25-Oct-12 14:24pm    
Oh, I understand. I'm a C guy, after all. Of course String.Empty is a superior choice (at least aesthetically).
Sergey Alexandrovich Kryukov 25-Oct-12 14:58pm    
Great.
Cheers,
--SA
fjdiewornncalwe 25-Oct-12 15:24pm    
+5. Correct plus Sergey's suggestion.
Thanks very much CPallini and Sergey. Yes I'm trying to remove all quotation marks so the replace with empty string method should work. Thanks for the resources links as well Sergey I'll have a play around with the code and try to make the code a bit more efficient. I'm only starting out with C# so these tips are invaluable.

Thanks,
Stephen.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900