2014-11-03 133 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

相關問題