Solved

VB.Net - Writing Strings to a comma delimited file

Posted on 2014-11-20
6
215 Views
Last Modified: 2014-11-20
I am attempting to write data out to a comma delimited file using a For - Next loop.
Some of the strings in the array use a double quote ( " ) as you would use to indicate Inches. The first few elements of the array do not have the double quote and they work fine, however, once I reach an element that contains the double quote the code treats it differently and I end up with a long string that does not handle the comma as a delimiter.

Here is the code:
Dim objWriter As New System.IO.StreamWriter(MeasDataFileName, True)

For j = 0 To maxAinIx - 2
    objWriter.Write(AinUnit(j, UnitsEMU))
    objWriter.Write(",")
Next
objWriter.WriteLine(AinUnit(j, UnitsEMU))

objWriter.Close()


I need help to ensure that the code will treat each element as a separate entry and no have the double quotes mess up the output.

Thanks,
Charlie
0
Comment
Question by:charlieb01
  • 2
  • 2
  • 2
6 Comments
 
LVL 33

Expert Comment

by:it_saige
ID: 40454927
What does the code for your AinUnit look like.  This is most likely what you want to change so that it can properly handle double-quotes.

-saige-
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 250 total points
ID: 40454935
Can you use the replace function and drop a chr(34) in there?

    objWriter.Write( Replace(AinUnit(j, UnitsEMU), chr(34), chr(34) & chr(34) & chr(34) )
0
 

Author Comment

by:charlieb01
ID: 40454960
The values in the AinUnit array come from a database. Some of them use the degrees symbol (for temperature), some use the double quotes (for inches - as in barometric pressure: "Hg) and some don't use any special symbol (such as the letter W  for watts). So I don't know how I can replace just the double quotes because it only affects some of the values.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 33

Accepted Solution

by:
it_saige earned 250 total points
ID: 40454982
So then the question is where do they get loaded into the array.  You want to modify setting the value in the array so that if a double quote is used you either:
1.  Replace it with an escape sequence (as shown by sirbounty).
2.  Encode it into another form (i.e. HTML Encoding)

The method used depends upon what is going to be receiving this file and how it processes it.  If you expect the double quotes to appear as double quotes in the file, then sirbounty has provided the way to do it, now we just need to know where to do it.

-saige-
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40455063
The replace function won't do anything if there are no quotes in the results...did you try the code?
0
 

Author Comment

by:charlieb01
ID: 40455668
The replace function did not work and I have run out of time to solve this problem. So, I am re-writing the database fields to use 'in' (for inches) to replace the double quotes.

Thank you for your help. Perhaps I will get a chance to revisit this issue again.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question