0

I have a string being read in and then outputted to a CSV file that has a comma inside of it. The string is

USA, B&W 1-25

All I want is for that string to stay in one particular cell and not be split into two different cells. There is probably an easy answer for this but I'm having trouble accomplishing this. Any help would be much appreciated. Here is my output code if need anyone wants to take a look at it...

    public void printAll()
    {
        output2.WriteLine("All companies in order of sequence number, THIS IS OUTPUT 2!");
        output2.WriteLine("___________________________________________________________________________________________");
        int i = listHead2;

        //Loops until the end of the list, printing out info
        while (i != -1)
        {
            output2.WriteLine("{0}" + ", {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20}, {21}, {22}, {23}, {24}, {25}, {26}, {27}, {28}, {29}, {30}, {31}, {32}, {33}, {34}, {35}",
                leaseName2[i], fieldName2[i],reservoir2[i], operator2[i], county2[i], state2[i],  majo2[i], resvCatgory2[i], disRate2[i], netOil2Int2[i], netGas2Int2[i], workingInt2[i], grossWells2[i]
                ,ultOil2[i], ultGas2[i], grossOil2[i], grossNGL2[i], grossGas2[i], netOil2[i], netGas2[i], netNGL2[i], revToInt2[i], operExpense2[i], totInvest2[i], revOil2[i], revGas2[i], operatingProfit2[i],
            revNGL2[i], discNetIncome2[i], seqNum2[i], wellID2[i], incASN2[i], lifeYears2[i],  ownQual2[i], prodTax2[i], AdValorem2Tax2[i]);
            i = pointers2[i];
        }
    }

Thanks.

Stock
  • 77
  • 3
  • 15

3 Answers3

3

You could try enclosing the field in double quotes: "USA, B&W 1-25" That should cause the string to be considered as one column.

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
  • +1 This is correct. See [this answer](http://stackoverflow.com/a/769713/4525) for a more complete solution. – harpo May 31 '13 at 15:44
  • @Erik Schierboom Sorry I wasn't clear, this is read in from a file and many different strings like this one that could have commas in them that I don't want to separate. – Stock May 31 '13 at 15:47
  • @Stock Well that would imply that the problem is in your source file. The source file doesn't have quotes around that field? – Erik Schierboom May 31 '13 at 15:48
  • More info: for a double quotes character in the string (for example `USA, "B&W" 1-25`) you should actually use `""`, so if it is a verbatim string you could write `@"""USA, """"B&W"""" 1-25"""`. – SimpleVar May 31 '13 at 15:48
3

You could create a small method that quoted the string:

public string QuoteString(string input)
{
    return "\"" + input.Replace("\"", "\"\"") + "\"";
}

You could then use this like:

QuoteString(leaseName2[i]), QuoteString(fieldName2[i]),...

Good luck with your code.

Casperah
  • 4,504
  • 1
  • 19
  • 13
  • Sorry, maybe you could help with this too. What if a string has multiple commas like BROMIDE, HUNTON, MIS what could I do there? – Stock May 31 '13 at 16:04
  • The quotes handles the commas, it would just be "BROMIDE, HUNTON, MIS" in one field – Casperah May 31 '13 at 21:37
0

CSV files don't necessarily need to be comma separated. You could use semicolon for separating if that helps!

SamiHuutoniemi
  • 1,576
  • 2
  • 16
  • 35