By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,444 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

Replacing character in a string

100+
P: 132
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:

Expand|Select|Wrap|Line Numbers
  1. int quotesCount = 0;
  2. for (int x = 0; x < line1.Length; x++)
  3. {
  4. if (line1[x] == (char)34) //quotes
  5. {
  6. quotesCount++;
  7. }
  8. if (line1[x] == (char)44) //comma
  9. {
  10. if ((quotesCount % 2 == 1) && (quotesCount > 1))
  11. {
  12. line1 = line1.Replace(line1[x], (char) 32); //replace comma with space character
  13. }
  14. }
  15. }
  16.  
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 :)
Jan 13 '09 #1
Share this Question
Share on Google+
8 Replies


vekipeki
Expert 100+
P: 229
String.Replace will replace all occurrences of a specified character with another character, so your line:
Expand|Select|Wrap|Line Numbers
  1. line1 = line1.Replace(line1[x], (char) 32);
is doing exactly that, replacing all commas with a space.

Since a String class is immutable (you cannot change it "in place"), consider using the StringBuilder Class (System.Text) and append character by character, while skipping the commas when needed.
Jan 13 '09 #2

100+
P: 132
Hi there.

I found another way to do this:

Expand|Select|Wrap|Line Numbers
  1. char[] tmpBuffer = line1.ToCharArray();
  2. tmpBuffer[x] = (char)32;
  3. line1 = new string(tmpBuffer);
  4.  
Hopefully this may be of use to somebody else.

Thank you.

Mark :)
Jan 13 '09 #3

Plater
Expert 5K+
P: 7,872
Are you doing something special that the comma cannot be there?
A comma inside a quote is part of the accepted CSV format.

Otherwise I guess what you're doing is going to work the best (some people suggest regex, but I have never found a CSV regex that ACTUALLY worked correctly)
Jan 13 '09 #4

vekipeki
Expert 100+
P: 229
If your actual problem is to parse a csv file, then why do you remove the commas first?

Just parse it on the fly, something like: find the opening quote, read all characters until you reach the closing quote (together with all the commas inside), then proceed to the next opening quote.

Right now you are looping through the whole thing twice, first time to remove the commas, second time to parse it, and at the end you don't have the commas (which are, as Plater said, accepted when put inside quotes).
Jan 14 '09 #5

100+
P: 132
@Plater
Hi there. I need to use particular vales from within each line of the CSV file, so I have used a string.split() method so I can grasp the respective string array items and use them later to create the values in a sql WHERE clause.

I am hence finding that the split command is also splitting within a string enclosed betwen quotes.

Thank you.

M :)
Jan 14 '09 #6

100+
P: 132
@vekipeki
No at the moment I am splitting the file, by seperator equal to comma but that is not good, hence the commas are staying and I am removing the rogue commas using the method highlighted above. Hence just one pass is being made at a time.

Your idea of reading it in by way of quotes could be a good idea though...

Thank you.

Mark :)
Jan 14 '09 #7

vekipeki
Expert 100+
P: 229
If it works, then it's great.

Just note that
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM SomeTable
  2. WHERE SomeColumn='a b c'
won't find columns where SomeColumn is 'a,b,c'.
Jan 14 '09 #8

Plater
Expert 5K+
P: 7,872
If you plan to use SQL on it, have you considered opening the CSV file with oledb (using the Jet provider)?
that's what I do, it just reads it into a DataSet for me.
Jan 14 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.