Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: arrays
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:
 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 25-Oct-12 7:01am
Edited 25-Oct-12 7:57am
v2
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you want to delete all "", just use System.String.Replace(string, string):
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:
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[^]. Smile | :)
 
—SA
  Permalink  
v5
Comments
CPallini at 25-Oct-12 15:10pm
   
5.
Sergey Alexandrovich Kryukov at 25-Oct-12 15:12pm
   
Thank you, Carlo.
--SA
Marcus Kramer at 25-Oct-12 15:23pm
   
+5. Nice.
Sergey Alexandrovich Kryukov at 25-Oct-12 15:24pm
   
Thank you, Marcus.
--SA
Espen Harlinn at 25-Oct-12 16:09pm
   
5'ed!
Sergey Alexandrovich Kryukov at 25-Oct-12 16:24pm
   
Thank you, Espen.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You may remove quotation marks using String.Replace method, replacing occurrences of double quote with the empty string, e.g.
string s = "\"name\"";
s = s.Replace("\"", string.Empty);
 

[changed "" with string.Empty, thanks to Sergey]
  Permalink  
v3
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 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 at 25-Oct-12 14:58pm
   
Great.
Cheers,
--SA
Marcus Kramer at 25-Oct-12 15:24pm
   
+5. Correct plus Sergey's suggestion.
Espen Harlinn at 25-Oct-12 16:09pm
   
5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 26 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100