locked
Remove double quotes from CSV file RRS feed

  • Question

  • Hi!

    I have CSV file and has dat alike below:

    "Tranfer","45","As per email, ""Data has been received, get it by Friday""".

    I want the result like below:

    "Transfer", "45","As per email, Data has been received, get it by Friday"

    So, I want to remove ("") quotes in the file, if it is any.

    How can I do this?

    Thanks,

     

     

    Wednesday, June 16, 2010 7:34 AM

Answers

  • Hi,

    You can simply call:

    .Replace("\"\"", "");

    on the string instance. However, if you plan to do more string manipulation, then I recommend using the System.Text.StringBuilder class instead. Here is a simple example:

    string contents;
    using (StreamReader sr = new StreamReader("C:\\MyCsv.csv"))
    {
      contents = sr.ReadToEnd();
      contents = contents.Replace("\"\"", "");
      sr.Close();
    }
    
    using (StreamWriter sw = new StreamWriter("C:\\MyCsv.csv"))
    {
      sw.Write(contents);
      sw.Close();
    }

    You can use a single FileStream object. E.g

    using (FileStream fs = new FileStream("C:\\MyCsv.csv", FileMode.Open, FileAccess.ReadWrite, FileShare.None))
    {
      StreamReader sr = new StreamReader(fs);
      string contents = sr.ReadToEnd();
    
      // replace "" with empty
      contents = contents.Replace("\"\"", "");
    
      // go back to the beginning of the stream
      fs.Seek(0, SeekOrigin.Begin);
    
      // adjust the length to make sure all original
      // contents is overritten
      fs.SetLength(contents.Length);
    
      StreamWriter sw = new StreamWriter(fs);
      sw.Write(contents);
      sw.Close();
    
      //fs.Close();
    }
    Hope this helps

    Jas

    • Marked as answer by Incredible15 Wednesday, June 16, 2010 3:04 PM
    Wednesday, June 16, 2010 9:06 AM

All replies

  • You can use the following code

     

    String st = "\"vamsi Krishna\"";
          st = st.Replace('"', ' ');
          st = st.Trim();
          return st;

    st.replace replaces double quotes with spaces and st.Trim() removes these spaces.

     

    hope this answers your question

    Please mark it is answer if this does answer your question. For more coding related help you can visit my blog C# Programming Help

    If this does not answer your question unpropose it

    • Proposed as answer by vamsi_krishna Wednesday, June 16, 2010 8:24 AM
    Wednesday, June 16, 2010 8:24 AM
  • Hi,

    You can simply call:

    .Replace("\"\"", "");

    on the string instance. However, if you plan to do more string manipulation, then I recommend using the System.Text.StringBuilder class instead. Here is a simple example:

    string contents;
    using (StreamReader sr = new StreamReader("C:\\MyCsv.csv"))
    {
      contents = sr.ReadToEnd();
      contents = contents.Replace("\"\"", "");
      sr.Close();
    }
    
    using (StreamWriter sw = new StreamWriter("C:\\MyCsv.csv"))
    {
      sw.Write(contents);
      sw.Close();
    }

    You can use a single FileStream object. E.g

    using (FileStream fs = new FileStream("C:\\MyCsv.csv", FileMode.Open, FileAccess.ReadWrite, FileShare.None))
    {
      StreamReader sr = new StreamReader(fs);
      string contents = sr.ReadToEnd();
    
      // replace "" with empty
      contents = contents.Replace("\"\"", "");
    
      // go back to the beginning of the stream
      fs.Seek(0, SeekOrigin.Begin);
    
      // adjust the length to make sure all original
      // contents is overritten
      fs.SetLength(contents.Length);
    
      StreamWriter sw = new StreamWriter(fs);
      sw.Write(contents);
      sw.Close();
    
      //fs.Close();
    }
    Hope this helps

    Jas

    • Marked as answer by Incredible15 Wednesday, June 16, 2010 3:04 PM
    Wednesday, June 16, 2010 9:06 AM
  • Thank you....

    I appreciate you...

    Wednesday, June 16, 2010 3:05 PM