2014-10-29 68 views
0

我想從我的下面的文本文件中刪除雙引號。我需要使用我的C#應用​​程序輸入文件並讀取文本文件,然後刪除所有雙引號並輸出結果。 我需要刪除所有的引號,並使它們成爲完美的四種格式 那麼我該如何做到這一點。如何從文本文件中刪除所有雙引號和完美格式的所有內容

輸入文件

Designator Comment Footprint Center-X(mm) Center-Y(mm) 

    "C5" "CAP,1n,50V.0603" "CAPC1608N" "97.733" "73.025" 
    "C4" "CAP,1n,50V.0603" "CAPC1608N" "99.638" "73.025" 
    "C3" "CAP,1n,50V.0603" "CAPC1608N" "101.543" "73.025" 
    "C7" "CAP1233,,1n,50V.0603" "CAPC1608N" "103.448" "73.025" 
    "C8" "CAP1233,1n,50V.0603" "CAPC1608N" "105.353" "73.025" 
    "C9" "CAP455,1n,50V.0603" "CAPC1608N" "107.258" "73.025" 
    "C10" "CAP4522,1n,50V.0603" "CAPC1608N" "109.163" "73.025" 

我的代碼從我的C#應用​​程序來輸入文本文件:

private void convert_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog ofd = new OpenFileDialog(); 
     ofd.InitialDirectory = @"C:\files"; 
     ofd.Filter = "TXT files (*.txt)|*.txt"; 
     ofd.FilterIndex = 2; 
     ofd.RestoreDirectory = true; 
     if (ofd.ShowDialog() == DialogResult.OK) 
     { 
      int[] cols = new int[] { 15, 30, 15, 20, 12 }; 
      string[] strLines = System.IO.File.ReadAllLines(fileName); 

      StringBuilder sb = new StringBuilder(); 
      string line = string.Empty; 
      for (int i = 0; i < strLines.Length; i++) 
      { 
       line = RemoveWhiteSpace(strLines[i]).Trim(); // here i am getting error in RemoveWhiteSpace, that it is not in the context 
       string[] cells = line.Replace("\"", "").Split(new string[] { " " }, StringSplitOptions.None); 
       for (int c = 0; c < cells.Length; c++) 
        sb.Append(cells[c].PadRight(cols[c])); 
       sb.Append("\r\n"); 
      } 

      //System.IO.File.WriteAllText(fileName, sb.ToString()); 
      MessageBox.Show(sb.ToString()); 
     } 
} 

我怎樣才能做到這一點?請幫助我。

我想獲得我輸入的所有文本文件的下面樣式的格式。所以對於上面的文本文件我想要得到的格式如下所示

 Designator Comment   Footprint Center-X(mm) Center-Y(mm) 

     C5  CAP,1n,50V.0603  CAPC1608N  97.733   73.025 
     C4  CAP,1n,50V.0603  CAPC1608N  99.638   73.025 
     C3  CAP,1n,50V.0603  CAPC1608N  101.543   73.025 
     C7  CAP,1n,50V.0603  CAPC1608N  103.448   73.025 
     C8  CAP,1n,50V.0603  CAPC1608N  105.353   73.025 
     C9  CAP,1n,50V.0603  CAPC1608N  107.258   73.025 
     C10  CAP,1n,50V.0603  CAPC1608N  109.163   73.025 

回答

1

嘗試這個例子,這將刪除雙引號並重新格式化您的文本。

private void Reformat(string fileName) 
{ 
    //Give the column width according to column index. 
    int[] cols = new int[] { 12, 35, 25, 15, 15 }; 
    string[] strLines = System.IO.File.ReadAllLines(fileName); 

    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(fileName, sb.ToString()); 
    MessageBox.Show(sb.ToString()); 
} 


private string RemoveWhiteSpace(string str) 
{ 
    str = str.Replace(" ", " "); 
    if (str.Contains(" ")) 
     str = RemoveWhiteSpace(str); 
    return str; 
} 
+0

來吧我不知道現在如何給我的代碼.. – 2014-10-30 14:24:54

+0

請參閱我的編輯代碼 – 2014-10-30 14:25:56

+0

這裏我在RemoveWhiteSpace中收到錯誤,它不是在上下文 – 2014-10-30 14:27:12

3

這兩個步驟是讀取文件並用空白替換引號。

string allText = File.ReadAllText(ofd.FileName); //read file 
string noQuotes = allText.Replace("\"", ""); //replace text 

字符串是不可變這就是爲什麼Replace返回一個新字符串對象,而不是簡單地改變現有allText字符串。

或者,您可能希望使用ReadAllLines,它將返回一個字符串數組,其中每個元素都是文本文件的一行。理論是一樣的,但你會遍歷數組並用空字符串替換每個元素中的引號。

如果你想使用ReadAllLines和WriteAllLines(保存文件出來),你可以這樣做

File.WriteAllLines(@"saveFile.txt", File.ReadAllLines(ofd.FileName) 
            .Select(s => s.Replace("\"", "")).ToList()); 

,將做閱讀,更換,並在一條線(不是更少數量的節約線條總是更好)。

+0

表示文件名不是在上下文 – 2014-10-29 20:48:31

+0

'Error4「System.Windows.Forms.OpenFileDialog」不包含「文件名」的定義和沒有擴展方法「文件名」,接受類型「系統的第一個參數。 Windows.Forms.OpenFileDialog'可以找到(你是否缺少使用指令或程序集引用?)' – 2014-10-29 20:49:00

+1

對不起,它應該是'FileName'大寫'N'(我已經更新了它的答案) – keyboardP 2014-10-29 20:49:04

2

我會做這種方式:

using System.IO; 

File.WriteAllText(@"C:/text2.txt", File.ReadAllText(@"C:/text1.txt").Replace("\"", "")); 

更新:更短的解決方案

2

你可以用正則表達式做到這一點很容易:

using System.IO; 
using System.Text.RegularExpressions; 

if (ofd.ShowDialog() == DialogResult.OK) 
{ 
    string withoutQuotes = Regex.Replace(File.ReadAllText(ofd.FileName), "\"", ""); 
    File.WriteAllText(ofd.FileName, withoutQuotes); 
} 
+0

這工作,但我的格式是去..我如何得到正確的格式 – 2014-10-29 20:54:08

+1

你能請詳細說明「正確的格式」? – 2014-10-29 20:56:07

相關問題