如何提取每个输出的最后一个值?

时间:2010-04-20 08:47:24

标签: c#

这是我的文本文件的内容,其中包含一个名称,如“output1”后跟一系列数字,“output2”后跟一系列数字等等由一个空格隔开...我想要提取每个输出的最后一个值。我该怎么做呢?即。我想将“output1”的值提取为29.933215(即“output2”之前的值)和“output2”的值为379.983559

  • 文本文件的内容如下所示:

output1 30.000000 29.999969 29.999907 29.999783 29.999535 29.999039 29.998047 29.996063 29.992133 29.988202 29.984273 29.980343 29.976414 29.972485 29.968556 29.964628 29.960700 29.956773 29.952846 29.948919 29.944992 29.941066 29.937140 29.933215 输出2 380.000000 379.999990 379.999970 379.999930 379.999850 379.999691 379.999372 379.998734 379.997469 379.996204 379.994940 379.993675 ​​379.992411 379.991146 379.989882 379.988617 379.987353 379.986088 379.984824 379.983559 output3 1.761964 1.761964 1.761964 1.761964 1.761964 1.761964 1.761964 1.761964 1.761963 1.761963 1.761963 1.761963 1.761963 1.761963 1.761963 1.761963 1.761962 1.761962 1.761962 1.761962 1.761961 1.761961 1.761961 1.761960

2 个答案:

答案 0 :(得分:2)

使用StreamReader的ReadToEnd方法。这将返回一个由您的数据组成的字符串。拆分字符串并将其存储到数组中。我想你可以从那里开始。

答案 1 :(得分:0)

     Dictionary<string, double> dic = new Dictionary<string,double>();
     string[] items = str.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
     string lastOutput = "";
     for(int index = 0; index < items.Length; index++)
     {
        if(items[index].Contains("output"))
        {
           if(lastOutput.Length > 0 && index > 0)
           {
              dic.Add(lastOutput, double.Parse(items[index - 1]));
           }
           lastOutput = items[index];
        }
     }
     dic.Add(lastOutput, double.Parse(items[items.Length-1]));