I have a .csv file in which each column and its content is separated by commas.
What I'm looking to do is be able to change each comma separator with a Pipping (|) and do this in C#.
I've googled but can't find anything concrete that can help me.
Thank you very much
Something like this should work (Insert inside a function) :
try/catch
I have put it for error handling, you may try to open a non-existent file and launch one or the otherException
.Remember to do
using System.IO;
in order to use the classFile
.Anti-quoting method, Version 2
Fixes:
In case of a reading error, it returns
null
and in case of a writing error, it returns thestring
modified one to avoid losing processed data.This other method is responsible for replacing the values of a... Better way ?
Considering that the content of your csv has commas inside values with quotes, with the previous solution they would also be replaced, so it is necessary to know the exact position of the quotes to ignore them:
What this last method does is that it ignores the values that are in quotes of type:
""
,''
and`
in order to leave the values completely in order, I have tried it with the following content in a csv:Note: The new method uses 3 arguments, the first is the csv address, the second is the delimiter, and the third is the new delimiter.
References:
string.Replace(string, string)
File.ReadAllText(string)
File.WriteAllText(string, string)
Cheers :)