2014-11-03 80 views
0

我正在写一个程序,在文本文件中写入一些名称,以符合我的第二个名称,正如您在此处看到的https://imageshack.com/i/eyW14lH6j。我的输出视图显示在链接中。 所以我所做的是,它的第二列逐行文本的读取行为和它根据MAX_PN列名称生成的名称。输出名称在不同的行上有所不同

对于例如:在上面的输出视图中,我需要任何时候有一个相同的字符串(100-0145来我需要旋转木马:4显示) 我想成为像第二列名称是相似的,我需要相同名称将出现在位置列。但是,对于我的代码,发生的情况是: ,如果在一两行三行之后出现相同的名称,则说明它不能读取。我该如何纠正这一点。请帮助我。

我的代码片段:

  int[] cols = new int[] { 15, 15, 25, 15, 15 }; 
      string[] strLines = System.IO.File.ReadAllLines(textBox1.Text); 

      StringBuilder sb = new StringBuilder(); 
      string line = string.Empty; 
      string LastComment = string.Empty; 
      string CarouselName = "Carousel"; 
      int iCarousel = 0; 

      for (int i = 0; i < strLines.Length; i++) 
      { 
       line = RemoveWhiteSpace(strLines[i]).Trim(); 
       string[] cells = line.Replace("\"", "").Split('\t'); 
       for (int c = 0; c < cells.Length; c++) 
        sb.Append(cells[c].Replace(" ", "_").PadRight(cols[c])); 

       if (cells.Length > 1) 
       { 
        if (cells[1] != LastComment & i > 0) 
        { 
         iCarousel++; 
         if (iCarousel > 45) 
          iCarousel = 1; 
         LastComment = cells[1]; 
        } 

        if (i == 0) 
         sb.Append("Location".PadRight(15)); 
        else 
         sb.Append(String.Format("{0}:{1}", CarouselName, iCarousel).PadRight(15)); 
       } 
       sb.Append("\r\n"); 
      } 

      System.IO.File.WriteAllText(textBox1.Text, sb.ToString()); 
+1

所以,你要读** ** MAX_PN从一个文本文件,然后分配给每个** ** MAX_PN号的独特* *轮播:x **其中** x **是一个数字? – mihai 2014-11-03 14:52:32

+0

@Mihai yes excatly .. – 2014-11-03 15:41:22

回答

1

尝试保存所有的名字在字典其中int是旋转木马的数量。 然后你可以查看什么轮播与你的名字相匹配。 看看这个例子:

Dictionary<string,int> namesForCarousels = new Dictionary<string,int>(); 

...

if (cells.Length > 1) 
{ 
    var name = cells[1]; 
    int carouselNumber; 
    if (namesForCarousels.TryGetValue(name, out carouselNumber) == false) 
     { 
      carouselNumber = iCarousel++; 
      namesForCarousels[name] = carouselNumber; 
     } 
    if (i == 0) 
    sb.Append("Location".PadRight(15)); 
    else 
    sb.Append(String.Format("{0}:{1}", CarouselName, carouselNumber).PadRight(15)); 
} 
+0

显示错误..'错误名称'namesForCarousels'在当前上下文中不存在' – 2014-11-03 15:43:51

+0

这是完美的..感谢很多.. – 2014-11-03 16:09:05

+0

你可以请一个厕所我的问题 2014-11-04 01:27:28

相关问题