2016-12-24 45 views
0

我有某些行我想選擇,陣列是的那些AB在陣列中選擇特定線

[0] A1 
[1] line I don't want to select 
[2] B1 
[3] line I don't want to select 
[4] A2 
[5] line I don't want to select 
[6] B2 
[7] line I don't want to select 
[8] line I don't want to select 
[9] line I don't want to select 
[10] A3 
[11] line I don't want to select 
[12] B3 

總是有含A含有B,因此在一行隔開的線和線這個例子中的那些行會[1][5][11]

我想選擇將包含A所有行,然後用含B各自的線將它們配對,並創建一個新的數組是這樣的:

[1] A1,B1 
[2] A2,B2 
[3] A3,B3 

回答

0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Select_specific_lines_in_an_array 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string line = ""; 
      int count = 0; 

      List<List<string>> testlist = new List<List<string>>(); 
      List<string> templist = new List<string>(); 

      System.IO.StreamReader file = new System.IO.StreamReader("inputfile.txt"); 
      while ((line = file.ReadLine()) != null) 
      { 
       if (line != "line I don't want to select") 
       { 
        Console.WriteLine(line); 
        templist.Add(line); 
        if (templist.Count == 2) 
        { 
         testlist.Add(templist); 
         templist = new List<string>(); 
        } 
       } 
       count = count + 1; 
      } 
      file.Close(); 
      Console.ReadKey(); 
     } 
    } 
} 

在while循環結束時testlist看起來就像這樣: enter image description here

注意這是不完美的代碼與它的問題是: - 它依靠它去A1 B1,如果它去了A1 A2,那麼程序仍然會運行 並且能夠工作,但它不會將A1和B1一起存儲,它將存儲 A1和A2在一起

但是我想這應該足以給ü出發點,以工作過。

也只是作爲一個最後一個音符,我用含有列表中的列表,而不是多維數組,因爲我們不知道在一開始許多個數據怎麼會有我們想要的。

編輯 - 樣品輸入文本

A1 
line I don't want to select 
B1 
line I don't want to select 
A2 
line I don't want to select 
B2 
line I don't want to select 
line I don't want to select 
line I don't want to select 
line I don't want to select 
A3 
line I don't want to select 
B3 
+0

你能後輸入文本的樣本。通常當你從一個文件輸入文本時,你正在閱讀多條記錄。因此,您首先必須確定每個記錄在文本文件中的起始位置,然後從開始或記錄中計算行數。當下一個記錄開始時,你必須再次開始計數。我已經做了40多年。並可以幫助,但需要看到原文。 – jdweng

+0

我只是做了一個文本文件,複製並粘貼自己的文字,他把他的提問開始,拿出的行號。此外計數我把在開始包住我需要它,但忘了把它拿出來當我最終沒有使用它。 – ryanmoir

1

嘗試正則表達式:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Text.RegularExpressions; 

namespace Select_specific_lines_in_an_array 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.txt"; 
     static void Main(string[] args) 
     { 
      string line = ""; 
      int count = 0; 

      List<List<string>> testlist = new List<List<string>>(); 
      List<string> templist = new List<string>(); 

      System.IO.StreamReader file = new System.IO.StreamReader(FILENAME); 

      string pattern = @"^(?'letter'[A-Z]+)(?'number'\d+)$"; 

      while ((line = file.ReadLine()) != null) 
      { 
       line = line.Trim(); 

       if (line.Length > 0) 
       { 
        Match match = Regex.Match(line, pattern); 

        if (match.Success) 
        { 
         Console.WriteLine("Letter : '{0}', Number : '{1}'", match.Groups["letter"], match.Groups["number"]); 
         templist.Add(line); 
         if (templist.Count == 2) 
         { 
          testlist.Add(templist); 
          templist = new List<string>(); 
         } 
        } 
       } 
       count = count + 1; 
      } 
      file.Close(); 
      Console.ReadKey(); 
     } 
    } 
}