2013-07-05 78 views
-6

大家好,我想在一个文本添加数据为3只列出控件使用C#添加一个文本文件中的每一行到一个列表C#

我有一个主要的额外和侧面列表和一个继承人的内容该文本文件

#main 
9622164 
90411554 
57568840 
53804307 
44095762 
89399912 
88264978 
26400609 
45725480 
53804307 
53129443 
51858306 
53797637 
91020571 
27415516 
57568840 
89185742 
20586572 
99594764 
19613556 
53797637 
75500286 
51858306 
89185742 
26400609 
90411554 
44330098 
91020571 
90411554 
47297616 
75500286 
28297833 
26400609 
27415516 
45725480 
53804307 
89399912 
89399912 
29401950 
9622164 
#extra 
79229522 
41517789 
80321197 
76774528 
44508094 
83994433 
45815891 
26593852 
74371660 
80117527 
80117527 
10389142 
10389142 
90726340 
51735257 
!side 
74530899 
74530899 
804000084 
70095154 
80344569 
24508238 
24508238 
5318639 
5318639 
15800838 
15800838 
35027493 
35027493 
54974237 
54974237 

我想在主目录下#main每一行的侧列表下!侧的每一行和额外的列表下#extra每行请帮助谢谢

这里是代码即时通讯使用atm将项目添加到列表,但我需要将它们分开

String text = File.ReadAllText("test.ydk"); 

    var result = Regex.Split(text, "\r\n|\r|\n"); 

    foreach (string s in result) 
    { 
     MainDeck.Items.Add(s); 
    } 
+1

[你尝试过什么(http://www.whathaveyoutried.com),具体而言,究竟是不是什么工作你试过了吗? –

+0

我曾尝试使用streamreader阅读的内容,但不能得到它的工作,我已经尝试搜索解决方案,但无法找到任何工作 – outlaw1994

+0

“,但不能得到它的工作”...什么都没有工作?任何具体的错误信息,怪异的输出?我们需要代码。 –

回答

0

假设您已经将其转换为字符串,您可以使用string.split()函数。这是使用C#。

sting yourtextfile; 
//make all the different sections the same 
yourtextfile.replace("#main", "#"); 
yourtextfile.replace("#extra", "#"); 
yourtextfile.replace("!side", "#"); 
//make all the arrays 
string[] all = yourtextfile.Split('#'); 
string[] main = all[0].Split('\n'); 
string[] extra = all[1].Split('\n'); 
string[] side = all[2].Split('\n'); 

之后,将数组转换为列表,并且您有三个您想要的列表。

+0

我到底该如何加入thnx – outlaw1994

+0

没有reliaze与!列表是什么列表是它在游戏中的一副扑克牌,并且我试图让一个转换器将上面的文本文件转换成可读的格式, – outlaw1994

0

使用File.ReadAllLines得到一个字符串数组的每一行的内容,然后遍历列表中的每个项目,设置,控制一个标志,它列出你(重新)添加每个项目。这不是最有效的,因为你必须处理每一行两次,但它非常干净,易于理解。

1

很难打败File.ReadLined这种方法 - 读取文件中的所有行。

比你可以迭代和以任何你需要的方式分裂。带着最后看到的所有物品名称的Foreach是合理的方法。

如果需要,使用Aggregate可以给出一个声明解决方案。花式版本样本使用不可变的Tuple在步骤之间传递值并使用IEnumerable来存储项目。几乎在功能上干净的代码(除TryParse因为没有合适的版本在框架存在):

int value; 
var dictionaryOfNumberByName = File.ReadLines(@"c:\myFile.txt") 
    .Aggregate(
    Tuple.Create("unknown", Enumerable.Repeat(Tuple.Create("", 0), 0)), 
    (all, line) => int.TryParse(line, out value) ? 
      Tuple.Create(
      all.Item1, 
      all.Item2.Concat(Enumerable.Repeat(Tuple.Create(all.Item1, value),1))) : 
      Tuple.Create(line, all.Item2), 
    all => all.Item2 
     .GroupBy(
      item => item.Item1, 
      (key, source) => Tuple.Create(key, source.Select(v => v.Item2).ToList())) 
     .ToDictionary(item => item.Item1, item => item.Item2)); 

普通代码应该简单地合计元素注入可变字典直接。

var dictionaryOfNumberByName = File.ReadLines(@"c:\myFile.txt") 
    .Aggregate(Tuple.Create("unknown", new Dictionary<string, List<int>>()), 
    (all, line) => { 
     int value; 
     if(!int.TryParse(line, out value)) 
     { 
      all.Item2.Add(line, new List<int>()); 
      return Tuple.Create(line, all.Item2); // switch current word 
     } 
     else 
     { 
     all.Item2[all.Item1].Add(value); 
     return all; 
     } 
}); 
+0

+1,我alwys忘记'Aggregate',比我愚蠢的Skip/Take回答要好得多 –

+0

@ sa_ddam213 - Aggregate是一条线,但如果不使用可变类型,它可以被认为是疯狂的并且不是真正有效的。你的解决方案看起来很容易理解(不是很理想,但容易证明工作)... –

2

您可以在线路使用LinqSkipTake如果你阅读使用File.ReadAllLines

示例文本文件:

// Read all the lines from the text file 
var textLines = File.ReadAllLines("c:\\stackoverflow.txt"); 

// Skip first line `#main` and take all lines until `"#extra` 
var mainItems = textLines.Skip(1).TakeWhile(x => !x.Equals("#extra")); 

// Skip items before `#extra`, and take all before `!side` 
var extraItems = textLines.SkipWhile(x => !x.Equals("#extra")).Skip(1).TakeWhile(x => !x.Equals("!side")); 

// Skip items before `!side` and take the rest 
var sideItems = textLines.SkipWhile(x => !x.Equals("!side")).Skip(1); 

我不会推荐这对于大的文本文件,但对于你应该没问题的例子。

0

有了这个,你可以有一个集合的所有值分隔:

Dictionary<string, List<string>> results = new Dictionary<string, List<string>>(); 
string line; 
int count = -1; 
using (var reader = File.OpenText(@"C:\Users\Jose\Desktop\bla.txt")) 
{ 
     while ((line = reader.ReadLine()) != null) 
     { 
      if (!Regex.IsMatch(line, @"\d")) 
      { 
       results.Add(line, new List<string>()); 
       count++; 
      } 
      else 
      { 
       results.ElementAt(count).Value.Add(line); 
      } 
     } 

} 
相关问题