Carla Fenech
Carla Fenech

Reputation: 19

Getting numerical amounts from csv files

Hi my application basically reads a CSV file which will always have the same format and I need the application to create a CSV file with different formatting. Reading and writing CSV file is not the issue, however the problem I am having is getting the amounts value as these are formatted with a , in the csv file (ex: 4, 500). Having said that these are being split when writing to csv file.

Ex: From the below, how can I get the full numbers .i.e. 2241.84 & 1072809.33

line = "\"02 MAY 18\",\"TTEWTWTE\",\"GRHGWHWH\",\"02 MAY 18\",\"2,241.84\",\"\",\"1,072,809.33\""

This is how I am reading from CSV file.

        openFileDialog1.ShowDialog();
        var reader = new StreamReader(File.OpenRead(openFileDialog1.FileName));
        List<string> searchList = new List<string>();
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            searchList.Add(line);
        }

So far I have tried to use the below which gets you \"2,241.84\" which is correct but when writing to csv file I am only getting 2

searchList[2].Split(',')[1].Replace("\"", "")

Upvotes: 0

Views: 101

Answers (2)

raidensan
raidensan

Reputation: 1139

Let me visualize contents in another way:

"
    \"02 MAY 18\",
    \"TTEWTWTE\",
    \"GRHGWHWH\",
    \"02 MAY 18\",
    \"2,241.84\",
    \"\",
    \"1,072,809.33\"
"

It seems that your separator is \", rather than ,. Change searchList[2].Split(',')[1].Replace("\"", "") to searchList[1].Split(new string[] { "\",\"" }, StringSplitOptions.None).

Upvotes: 1

SᴇM
SᴇM

Reputation: 7213

In your case you can use this:

var result = searchList[2].Split(new string[] { "\",\"" }, StringSplitOptions.None)[4].Replace("\"", "");

Split your string with "," separator, instead of ,.

I don't know why you are using static numbers for indexes, but I will assume it's for test purposes.

Upvotes: 0

Related Questions