从2D数组中删除引号 [英] Remove Quotation Marks from 2D Array

查看:79
本文介绍了从2D数组中删除引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我目前正在尝试创建一个程序,该程序可以使用逗号分隔的文本文件,该文本文件具有许多行,每个单词都用逗号包围.该文件的布局如下:

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"



我想使用文件的每个单词,并将它们另存为2D数组中的单独条目.我已经能够编写代码了,我遇到的麻烦是我也想删除条目周围的引号.到目前为止,我的代码是:



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);
            }
         }


任何人都可以建议添加什么以从每个allLines单元中删除所有"吗?


Can anyone suggest what to add to this to remove all the " " from each allLines cell?

推荐答案

如果要删除所有",只需使用System.String.Replace(string, string) :
If you want to delete all "", just use System.String.Replace(string, string):
string myString; //...
//...
myString = myString.Replace(@"""", string.Empty); // "" with empty string


请参阅:
http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx [ ^ ].

如果您需要使用一些复杂的规则来删除引号(通常是这种情况),请使用类System.Text.RegularExpressions.Regex:
来学习和使用正则表达式 http://en.wikipedia.org/wiki/Regular_expressions [ http://msdn.microsoft.com/en-us/library/system. text.regularexpressions.regex.aspx [ ^ ].

请看我对这个问题的评论.目前尚不清楚问题和源代码与您声明的多维数组之间的关系.可能关系不大.

现在,有关重用对话框的奖励建议.始终从头开始创建它是不好的,因为您失去了它的位置以及它先前状态的所有其他方面.最通用,最可靠的方法是以最原始的形式使用惰性评估模式:
http://en.wikipedia.org/wiki/Lazy_evaluation [


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



这比在声明类的构造中完成所有操作要好得多.对于Forms来说,这不是什么大问题,但是对于WPF而言,问题在于,如果尚未正确初始化常规UI,那么可能还为时过早,因此在所有情况下,惰性模式都是绝对的赢家. >
为了获得更多的乐趣,您可以熟悉更笼统的(我承认,也许太深奥了)我希望您在这里……只有一次"我提出的模式:
希望您在这里……只有一次 [



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


您可以使用String.Replace方法删除引号,用空字符串替换双引号,例如
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);




[由于谢尔盖(Sergey)将""更改为string.Empty]




[changed "" with string.Empty, thanks to Sergey]


非常感谢CPallini和Sergey.是的,我正在尝试删除所有引号,因此用空字符串替换方法应该可以工作.感谢您提供的资源链接,谢尔盖(Sergey),我将试用一下代码,并尝试使代码更高效.我只是从C#开始,所以这些技巧非常宝贵.

谢谢,
斯蒂芬.
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.


这篇关于从2D数组中删除引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆