string parsing question

G

Guest

I have a question.
I'm reading a CSV file that is uploading to my SQL db, I'm parsing out the
file line by line. I'm getting the values and putting them into an arrayList
seperate by commas. The problem I'm having is that one of the data values has
commas in it so its blowing up on the other fields. How can I remove the
commas from my string if they exist?

example:
Getting this
BMW, Used, 325C, $19,252.00, Smith

due to the comma in 19,252.00 its thinking its a new value at 252
So if this is the case how can I remove the commas in arraylist[4] data item?
Now that field will not have a comma all the time or will it be in the same
place it can have 1,252,252.00 or 1252.00 or 5,252.00 . is this possible to
do?
 
P

PeterKellner

I have a question.
I'm reading a CSV file that is uploading to my SQL db, I'm parsing out the
file line by line. I'm getting the values and putting them into an arrayList
seperate by commas. The problem I'm having is that one of the data values has
commas in it so its blowing up on the other fields. How can I remove the
commas from my string if they exist?

example:
Getting this
BMW, Used, 325C, $19,252.00, Smith

due to the comma in 19,252.00 its thinking its a new value at 252
So if this is the case how can I remove the commas in arraylist[4] data item?
Now that field will not have a comma all the time or will it be in the same
place it can have 1,252,252.00 or 1252.00 or 5,252.00 . is this possible to
do?

String.Replace(.. shoud work. Am I missing something here?
Peter Kellner
http://peterkellner.net
 
B

bruce barker \(sqlwork.com\)

assuming only the dollar field has extra commas then


string[] fieldParse = line.Split(',');
for (int i=0; i < 4; ++i) fields = fieldParse;
for (int i=4; i < fieldsParse.Length-1; ++i) fields[3] += "," +
fieldParse;
fields[4] = fieldParse[fieldParse.Length -1];

-- bruce (sqlwork.com)
 
S

Steven Cheng[MSFT]

Hi igotyourdotnet,

I think the problem here is that both the cell(column)'s separator and the
currency's separator use the comma. I'm wondering whether your CSV sheet's
table structure is fixed, in other words whether the column count is fixed
and the currency($.xxxx) will always occur at the fixed column index in the
columns of each row?

If this structuer is fixed , your string path can use the following logic
to pickup the corret columns from each string line:

1) use string.Split to get the string array separated by comma

2) read the prior columns before the currency column

3) read the last columns (from the end of the array) which is behind the
currency column

4) and for those array items left, they should be recombined to construct
the currency value.

Please feel free to post here if you have any other concerns or ideas.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top