English 中文(简体)
C#: 检查阿雷拉派教徒 具体类型
原标题:C#: Checking That ArrayList Elements have specific type
               ArrayList fileList = new ArrayList();


      private void button2_Click(object sender, EventArgs e)
            {
          if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
            string line;

            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog1.FileName);
            while ((line = file.ReadLine()) != null)
            {
                // Puts elements in table
                fileList.Add(line.Split( ; ));
            }

            file.Close();
        }

        for (int i = 0; i < fileList.Count; i++)
        {
            for (int x = 0; x < (fileList[i] as string[]).Length; x++)
            {
               // if (x ==0)
              //  { 
                    //fileList[0] must Be int 
              //  }
               // if (x==1)
                    //fileList[1] must be string 

                this.textBox2.Text += ((fileList[i] as string[])[x] + " ");
            }

            this.textBox2.Text += Environment.NewLine;
        }
    }

到目前为止,我在此开会。

我从CSV档案中拿到这些内容。

现在我需要确保,1个<>街区只有编号(1,2,3,4,5),第二个<>街区<>只拥有姓名(因此,其类型或特性)、第三个姓等。 等等。

The rows are presented as this : 1; George;Mano;

我如何确保CSV档案有正确的类型?

我认为,有关这个问题的任何进一步法典都将放在声明的2号内。

非常感谢你,

George。

最佳回答

www.un.org/Depts/DGACM/index_spanish.htm Edit:既然我们确实知道它,这里的答案是一栏;-

www.un.org/Depts/DGACM/index_french.htm

for (int i = 0; i < fileList.Count; i++)
{
   string[] lineItems = (string[])fileList[i];
   if (!Regex.IsMatch (lineItems[0], "^d+$")) // numbers
      throw new ArgumentException ("invalid id at row " + i);
   if (!Regex.IsMatch (lineItems[1], "^[a-zA-Z]+$")) // surnames - letters-only
      throw new ArgumentException ("invalid surname at row " + i);
   if (!Regex.IsMatch (lineItems[2], "^[a-zA-Z]+$")) // names - letters-only
      throw new ArgumentException ("invalid name at row " + i);
}
问题回答

我认为,你的问题需要做更多的工作。

页: 1 不管怎样,没有理由将其转换为string[],只是要花时间。 不管其类型如何,时间长度不变。 我们不能利用这种方法确定哪些项目正在铺设。

你们需要穿透这些物品,看看它们是否只包含数或数。

而且,你读CSV文档的代码不正确。 CSV文档是混合式的。 而且,它们有可能在双重引用中含有 com。 这些漫画应当被忽视。 http://www.blackbeltcoder.com

www.un.org/Depts/DGACM/index_french.htm

String

Checking the value of the first line read and trying to convert the string into an integer would be a valid approach.

Your current approach is adding the String that is returned by System.IO.StreamReader.ReadLine into your collection which you later turn into a String[] by using the String.Split method.

你的其他要求将更加困难,因为你们所阅读的每一个线都是已经存在的。 因此,你必须在座标内审视每一特性,以确定它是否为名字。

换言之,你可能想找到一种不同的方式来提供投入。 我同意,定期表达可能是消除黄麻数据的最佳途径。

http://msdn.microsoft.com/en-us/library/system.text.tableexpressions.regex.aspx”rel=“nofollow”

档案管理员必须 页: 1

int x;

if(int.TryParse (fileList [0], out x){/do what here and x will have that integer Value. 如果TryParse没有燃烧,那么如果不会燃烧,就会退回。

档案资料(l)必须说明:

传票是传票。 见char. 适当方法。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...