Click here to Skip to main content
12,997,060 members (75,795 online)
Rate this:
Please Sign up or sign in to 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:

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 6:01am
Updated 25-Oct-12 6:57am
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.
Rate this: bad
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:[^].

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:[^],[^].

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:[^].

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[^]. :-)

CPallini 25-Oct-12 15:10pm
Sergey Alexandrovich Kryukov 25-Oct-12 15:12pm
Thank you, Carlo.
Marcus Kramer 25-Oct-12 15:23pm
+5. Nice.
Sergey Alexandrovich Kryukov 25-Oct-12 15:24pm
Thank you, Marcus.
Espen Harlinn 25-Oct-12 16:09pm
Sergey Alexandrovich Kryukov 25-Oct-12 16:24pm
Thank you, Espen.
Rate this: bad
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]
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... :-)
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.
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
Marcus Kramer 25-Oct-12 15:24pm
+5. Correct plus Sergey's suggestion.
Espen Harlinn 25-Oct-12 16:09pm
Rate this: bad
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.


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 26 Oct 2012
Copyright © CodeProject, 1999-2017
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