String 我尝试使用字符串拆分,每行数据中的元素数量不同。我无法在末尾跟踪NULL

String 我尝试使用字符串拆分,每行数据中的元素数量不同。我无法在末尾跟踪NULL,string,split,String,Split,试图从数据行数为的数据文件中读取数据,每行的元素数都在变化 StreamReader read = new StreamReader("TextFile1.txt"); string str1 = " "; while (str1 != null) { str1 = read.ReadLine(); if (str1 != null) {

试图从数据行数为的数据文件中读取数据,每行的元素数都在变化

        StreamReader read = new StreamReader("TextFile1.txt");

        string str1 = " ";
        while (str1 != null)
        {
            str1 = read.ReadLine();
            if (str1 != null)
            {
                richTextBox1.AppendText("\n"+str1);
                string[] s = str1.Split(' ');
                i = 0;
                sum = 0;
                while (s[i] != null)
                {
                    if(i>0)
                        j=int.Parse(s[i]);
                    sum = sum + j;
                    i = i + 1;

                } 


            }

        }

假定您发布的代码是C#,您应该使用
String.length
属性

只是循环内部的一个片段:

            string[] s = str1.Split(' ');
            i = 0;
            sum = 0;
            while (i < s.length)
            {
                j=int.Parse(s[i]);
                sum = sum + j;
                i = i + 1;

            }
string[]s=str1.Split(“”);
i=0;
总和=0;
而(i

顺便说一下,我删除了
if(I>0)
条件,因为它似乎不必要。但是,如果您想排除行中的第一个元素(这就是
if(i>0)
条件所做的),那么只需更改
i=0
i=1

请编辑您的问题并在其中添加语言标记。看起来像C。行动,行动,请确认。