Reputation: 467
How I can remove comma ',' between each " " in a string with C# ?
I have try with:
string result = Regex.Replace(input,
@",([^""]$)",String.Empty);
but it doesn't work ...
Thank
Upvotes: 1
Views: 4969
Reputation: 467
// extract the fields
Regex CSVParser = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
String[] Fields = CSVParser.Split(Test);
// clean up the fields (remove " and leading spaces)
for (int i = 0; i < Fields.Length; i++)
{
Fields[i] = Fields[i].TrimStart(' ', '"');
Fields[i] = Fields[i].TrimEnd('"');
}
Upvotes: 0
Reputation: 14522
My idea is to split by "quotes" so each uneven-index element will be something between quotations. Going through those, replacing commas with nothing for them, and then rejoining everything to a single string, should do the trick:
var parts = input.Split('"');
for (var i = 1; i < parts.Length; i += 2)
{
parts[i] = parts[i].Replace(",", "");
}
var result = string.Join("\"", parts);
Upvotes: 6
Reputation: 11519
I'm slightly confused on what your input string looks like, but as long as you are trying to do simple replace of commas you could try
var result = input.replace(",","");
Upvotes: 1