【问题标题】:Save sections of text data to arrays将部分文本数据保存到数组
【发布时间】:2014-04-15 17:47:28
【问题描述】:

我有数据看起来有以下格式。我想为每个“运行”创建一个新的 2 列数组,并在 : 之后使用相应的运行名称。

Ran:    Efg    (space between : and E)
1       50     (tab delimited)
2       52     (tab delimited)
3       54     (tab delimited)
Ran:    pg2    (space between : and E)
1       40     (tab delimited)
2       60     (tab delimited)
3       80     (tab delimited)
Ran:    2      (space between : and E)
1       14     (tab delimited)
2       15     (tab delimited)
3       16     (tab delimited)

到目前为止,我在使用以下内容时遇到了问题,因为它给了我一个无限循环,但我不确定如何从之前访问字符串 resulttext

public void openDVHToolStripMenuItem_Click (object sender, EventArgs e){....}

public static string resulttext
    {
        get { return resulttext; }
    }

其次,到目前为止,我有以下内容来分隔待定数组的名称。

 private void button1_Click(object sender, EventArgs e)
    {
        Dictionary<string, List<string>> Darray = new Dictionary<string, List<string>>();

        using (StringReader reader = new StringReader(resulttext))
        {
            string line;
            string ran = "Ran:";
            string region = "";

            while (null != (line = reader.ReadLine()))
            {
                if (line.Contains(ran))
                {
                    string[] splitHeader = line.Split(":".ToCharArray());
                    region = splitHeader[1].Trim();
                }
               else
               {
                   if (!line.Contains(ran))
                   {
                       List<string> Dlist = new List<string>();
                       Darray.Add(region, Dlist);
                    }

                }

            }
            foreach (var item in Darray)
            {
                richTextBox2.AppendText(item + Environment.NewLine);
            }

        }


    }

我必须能够稍后调用每个数组并对两列数字的值进行一些基本的数学运算。

有什么建议吗?

【问题讨论】:

  • 是什么让你陷入无限循环?
  • 为什么不将整个文件读入 List 或 Tuple,然后在指定的 delim 上设置 Splits() 的正确代码行,然后决定如何通过收集和处理。我建议查找如何将文件加载到字典或 HashSet、HashList、HashTable 等的 List 中。有很多选项,然后从那里处理数据..
  • @selman22,使用 get { return resulttext; } 导致 Visual Studio 给我一条消息,说明这是一个无限循环。

标签: c# arrays winforms dynamic-arrays


【解决方案1】:

这个:

public static string resulttext
{
    get { return resulttext; }
}

给你一个“无限循环”,因为它是递归的; return resulttext 致电 public static string resulttext。这样做:

public static string ResultText { get; private set; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多