2014-11-14 68 views
3

我是一名初學C#程序員,並對我正在構建的應用程序提出了一個簡短問題。我的進程讀入多個文件,目的是根據文本文件中的1或0管道分隔字段剝離特定記錄。它實際上是文件中最後一個分隔的字段。如果它是0,我將它寫入一個臨時文件(稍後將替換我讀的原文),如果它不是我的東西。不要試圖讓它太混亂,但文件中有兩種類型的記錄,即標題行,然後是幾行支持行。標題行是唯一具有標誌的行,因此從下面可以看出,如果bool被設置爲0的好記錄,它會將標題記錄與其下的所有supp記錄一起寫入,直到它遇到一個壞的一個在這種情況下它會否定寫下他們,直到下一個好的。C#從字符串拆分中刪除最後一個分隔的字段

但是,我現在想要做的(想知道最簡單的方法)是如何在沒有最後一個管道分隔字段(IE標誌)的情況下編寫標題記錄。由於它應該始終是該行的最後2個字符(例如,需要先行管道時爲「0 |」或「1 |」),它是否應該是我的inputrecord字符串上的字符串修剪?有更容易的方法嗎?有沒有辦法在記錄上進行拆分,但實際上並沒有包含最後一個字段(在本例中是字段36)?任何意見,將不勝感激。謝謝你,

static void Main(string[] args) 
{ 
    try 
    { 
     string executionDirectory = RemoveFlaggedRecords.Properties.Settings.Default.executionDirectory; 
     string workDirectory = RemoveFlaggedRecords.Properties.Settings.Default.workingDirectory; 

     string[] files = Directory.GetFiles(executionDirectory, "FilePrefix*");    
     foreach (string file in files) 
     { 
      string tempFile = Path.Combine(workDirectory,Path.GetFileName(file)); 
      using (StreamReader sr = new StreamReader(file,Encoding.Default)) 
      { 
       StreamWriter sw = new StreamWriter(tempFile); 
       string inputRecord = sr.ReadLine(); 

       bool goodRecord = false; 
       bool isheaderRecord = false; 
       while (inputRecord != null) 
       { 
        string[] fields = inputRecord.Split('|'); 
        if (fields[0].ToString().ToUpper() == "HEADER") 
        {        
         goodRecord = Convert.ToInt32(fields[36]) == 0; 
         isheaderRecord = true; 
        } 

        if (goodRecord == true && isheaderRecord == true) 
        {  
         // I'm not sure what to do here to write the string without the 36th field*** 
        } 
        else if (goodRecord == true) 
        { 
         sw.WriteLine(inputRecord); 
        } 

        inputRecord = sr.ReadLine(); 
       } 

       sr.Close(); 
       sw.Close(); 
       sw = null; 
      }  
     } 

     string[] newFiles = Directory.GetFiles(workDirectory, "fileprefix*"); 
     foreach (string file in newFiles) 
     { 
      string tempFile = Path.Combine(workDirectory, Path.GetFileName(file)); 
      string destFile = Path.Combine(executionDirectory, Path.GetFileName(file)); 

      File.Copy(tempFile, destFile, true); 
      if (File.Exists(destFile)) 
      { 
       File.Delete(tempFile); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex); 
    } 

    finally 
    { 
     // not done 
    } 
} 
+0

你可能分裂,使用鏈接的'拿(計數)'取除了最後一個字段,然後再次加入它來寫出... – 2014-11-14 19:54:23

回答

3

一種方法可以做到這一點 - 如果你想在代碼中的那點東西是永遠寫所有,但最終的元素在你的string[] - 是構建一個for循環前的最後終止項目:

for (int i = 0; i < fields.Length - 1; i++) 
{ 
    // write your field here 
} 

這是假設你要單獨寫的每一個領域,你想通過fields首先進行迭代。如果你想要做的就是寫一個字符串單行,而無需使用一個循環,你可以這樣做:

var truncatedFields = fields.Take(fields.Length - 1); 

然後只寫truncatedFieldsstring[]你認爲合適的。一種方法,你可以在一條線上完成所有這些:

sw.WriteLine(String.Join("|", fields.Take(fields.Length - 1))); 
+0

'string.Join'將分隔符作爲第一個參數,並將字符串集合作爲第二個。 – juharr 2014-11-14 20:16:17

+0

@juharr糟糕 - 謝謝。 – furkle 2014-11-14 20:16:38

+0

此外,分隔符應該是一個字符串 – juharr 2014-11-14 20:17:02

0

goodRecord = fields.Last()。Trim()==「0」;

如果(inputRecord.Contains( 「|」)字符串outputRecord = inputRecord.Substring(1 inputRecord.LastIndexOf( 「|」));

相關問題