Solved

VB.Net - Writing Strings to a comma delimited file

Posted on 2014-11-20
6
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 34

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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 34

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

695 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