473,320 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Replacing character in a string

132 100+
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
8 4727
vekipeki
229 Expert 100+
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
E11esar
132 100+
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
7,872 Expert 4TB
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
229 Expert 100+
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
E11esar
132 100+
@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
E11esar
132 100+
@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
229 Expert 100+
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
7,872 Expert 4TB
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

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

Similar topics

10
by: Xah Lee | last post by:
i have a bunch of java files that has spaced-out formatting that i want to get rid of. I want to replace two end of line characters by one end of line characters. The files in question is unix, and...
4
by: C# Learner | last post by:
What's the "standard" way of replacing a character in a string? Obviously, I can't say "myString = character;" because strings are immutable... So what would be the "standard" way of doing this?...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
5
by: Jim Lawton | last post by:
Hello group, I'm not very experienced in C#, but I can't find any example of this (common) programming problem. I have a string which contains a repeated substring - lets say it's like :- ...
7
by: VMI | last post by:
If I have the string "Héllo", how can I replace char (é) with an 'e'? I cannot use the String.Replace() fuction. It has to be by replacing one char with another. Thanks.
12
by: anonymous | last post by:
Hello, I need to replace this char  with another char. However I am not able to acieve this. I tried this but it doesnt work: str = str.Replace(chr(asc(194)), "") Can somebody help ?
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
2
by: Alain | last post by:
Hi, I am working on a project where I need to convert international characters with acii values. Like André -> andre and Björn -> bjorn. How can I do this without replacing every single...
3
by: John | last post by:
Hi How can I replace all occurrences of a character in a string to another character? Thanks Regards
2
by: RJSF | last post by:
Hi, I have problem, where I need to replace the 8th character in a varchar string eg 002556-108823-AN-02 ie I need to change the 1 to 3. (and 2s to 4s in other strings eg 040598-20066-AN-02) ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.