2010-04-20 83 views
0

這裏是我的文本文件的內容,其中包含一個名稱,如「output1」後面跟着一系列數字,「output2」後面跟着一系列數字等等,由一個空格分隔......我想提取每個輸出的最後一個值。我該怎麼做呢? 。也就是說,我想提取「輸出1」的值(即之前「輸出2」的值。)如下圖所示29.933215和「輸出2」的文本文件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 OUTPUT2 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.9886 17 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

+0

是否outputX始終以新行開始? – 2010-04-20 08:51:33

回答

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])); 
2

使用的StreamReader的ReadToEnd的方法。這將返回一個由您的數據組成的字符串。拆分字符串並將其存儲到數組中。我想你可以從那裏出發。

+0

+1不適合用勺子給程序員提供應該很容易做的代碼。 – 2010-04-20 09:08:51

相關問題