Solved

VB.Net - Writing Strings to a comma delimited file

Posted on 2014-11-20
6
222 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

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

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…

726 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