Solved

VB.Net - Writing Strings to a comma delimited file

Posted on 2014-11-20
6
212 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 32

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 32

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now