title: Removing trailing space from CSV in C#
body: <p>I have a CSV file with a lot of trailing spaces in some columns, so I'm trying to write a simple Windows Forms Application that loops through all lines in my CSV and removes the trailing spaces. I think I'm pretty far, but I have the feeling that C# sees this as one line:</p>
  
  <pre><code>"Lorem ipsum dolor sit amet","Default ",9999,1,"base "
  </code></pre>
  
  <p>While it's supposed to see this as one line:</p>
  
  <pre><code>"Lorem ipsum dolor sit amet"
  </code></pre>
  
  <p>This is the code I have right now:</p>
  
  <pre><code>String path = @"C:\Users\me\Documents\test.csv";
  
  string[] allLines = File.ReadAllLines(path);
  using (StreamWriter sw = new StreamWriter(path))
  {
      foreach (string line in allLines)
      {
          if (!string.IsNullOrEmpty(line) &amp;&amp; line.Length &gt; 1)
          {                
              line.TrimEnd(' ');
  
              sw.WriteLine(line);
              //sw.WriteLine(line.TrimEnd(' '));
          }
      }
  }
  Console.WriteLine(allLines);
  Console.WriteLine("Done");
  </code></pre>
  
  <p>How do I make sure that in the case my CSV file is like this:</p>
  
  <pre><code>"Lorem ipsum dolor sit amet ","Default ",9999,1,"base "
  "simple","Default ",9999,1," base"
  "test ","Default ",9999,1,"base"
  </code></pre>
  
  <p>It comes out after running the C# code like this (so without the spaces at the end and the beginning):</p>
  
  <pre><code>"Lorem ipsum dolor sit amet","Default ",9999,1,"base"
  "simple","Default",9999,1,"base"
  "test","Default",9999,1,"base"
  </code></pre>
  
  <p>Edit: I also tried doing <code>line = line.TrimEnd(' ');</code> but this gives me the error Cannot assign 'line' because it is a 'foreach iteration variable'.</p>