Click here to Skip to main content
15,886,565 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to replace , with a "," in my .csv string.

For Example:

Change this
1,S0000093,GNL-000099,Aarpee Hygiene Pvt Ltd,H0010AW0101,G1150505010,G1150504070406,14,GNL-005111,,35637153767

to this
1,"S0000093","GNL-000099","Aarpee Hygiene Pvt Ltd","H0010AW010","G1150505010","G1150504070406","14","GNL-005111","","35637153767"

I have been trying the line.Replace("\"","\"\"")+"\""; but it get anything working properly.

Any help would be appreciated....Thanks in Advance...

my code

C#
private void ExportToCsv(string fileOut)
        {
            string text = ",";
            string text2 = "Data Source=" +
                (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) +
                "\\freedom.sdf;Persist Security Info=False";
            SqlCeConnection sqlCeConnection = new SqlCeConnection(text2);
            sqlCeConnection.Open();
            string text3 = "select * from Test";
            SqlCeCommand sqlCeCommand = new SqlCeCommand(text3, sqlCeConnection);
            SqlCeDataReader dr = sqlCeCommand.ExecuteReader();
            DataTable schemaTable = dr.GetSchemaTable();
            StreamWriter streamWriter = new StreamWriter(fileOut, false);
          
            streamWriter.WriteLine(string.Format("Sr.No,Sales Order No,Shipment No,Cust. Name,Item,Bundle No.,Roll No.,Qty,Trans Id,Validated By,Record Id", new object[0]));
            while (dr.Read())
            {
                string text4 = "";
                for (int i = 0; i < dr.FieldCount; i++)
                {
                    object obj = dr.GetValue(i);
                    if (obj != null)
                    {
                        text4+= obj.ToString();
                    }
                    else
                    {
                        text4 = (text4 ?? "");
                    }
                    if (i < dr.FieldCount - 1)
                    {
                        //text4 += text;
                        text4 += "\"" + text.Replace("\"", "\"\"") + "\"";
                        
                    }
                }
                streamWriter.WriteLine(text4);
            }
            streamWriter.Close();
            sqlCeConnection.Close();
        }
Posted
Updated 7-Jul-15 22:08pm
v3

You are not replacing what you claim to replace.

You need to replace , with ",": line.Replace(",", @""","""); then you can add missing "" at the beginning and the end, using string.Format, not inefficient string concatenation (strings are immutable, that's why).

Before, you got alternative solution where you first split the string by "," and then join the array in new string again, this time adding quotation marks. Why did you ignore this solution?

After all, all those string manipulation and CSV itself is low-tech you have to solve by yourself, to tech yourself at least some programming. Just use some patience and attention for detail. And use the debugger.

—SA
 
Share this answer
 
v3
Change this line

C#
text4 += "\"" + text.Replace("\"", "\"\"") + "\"";


to this

C#
text4 += "\"" + text4.Replace("\"", "\"\"") + "\"";
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900