2016-12-24 96 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(); 
     } 
    } 
}