Hi there.
I am looping through a csv file and for each line, I need to replace a comma in a string that occurs between quotes.
For example:
"The", "cat", "sat", "on, the", "mat"
Here I need to replace "on, the" with "on the".
My first draft solution is to count the quotes and if odd, the next comma is rogue. My first draft solution is as follows:
This solution will replace all commas for a space character but I only want to replace the specific character for a given position value, here x.
Does anybody know how I can do this please?
Thank you.
M :)
I am looping through a csv file and for each line, I need to replace a comma in a string that occurs between quotes.
For example:
"The", "cat", "sat", "on, the", "mat"
Here I need to replace "on, the" with "on the".
My first draft solution is to count the quotes and if odd, the next comma is rogue. My first draft solution is as follows:
Code:
int quotesCount = 0; for (int x = 0; x < line1.Length; x++) { if (line1[x] == (char)34) //quotes { quotesCount++; } if (line1[x] == (char)44) //comma { if ((quotesCount % 2 == 1) && (quotesCount > 1)) { line1 = line1.Replace(line1[x], (char) 32); //replace comma with space character } } }
Does anybody know how I can do this please?
Thank you.
M :)
Comment