Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
photo of Dan Mabbutt

Dan's Visual Basic Blog

By Dan Mabbutt, About.com Guide to Visual Basic since 2002

Join, Arrays, and CSV files

Friday September 5, 2008

I've previously written about how handy the Split function is in the article, Amazing Splits. But I recently discovered that the companion function, Join can be pretty useful too.

In the back of my mind, I had the idea that Join was just simple concatenation, like the & operator. In fact, it only works on arrays of strings. This makes it ideal for creating a CSV file - Comma Separated Values, like those commonly used as input to Excel and other programs. (For those who haven't discovered the joys of XML yet.)

To make it just a bit more challenging, I wrote a program that joins an array of strings into a single string in proper CSV file format and writes it to a sequential file - in just one statement.

Quick ... Don't look ... see if you can write just one statement that does all that.

Wanna look? Here's mine.

My.Computer.FileSystem.WriteAllText( _
   My.Computer.FileSystem.CurrentDirectory & _
   "\CSVFile.txt", _
   """" & Join(ArrayOfStrings, """,""") & """", _
   False)

(This isn't only about writi ng interesting code. In the case above, the compiler does work that would ordinarily be done every time the code is executed. If this code were in a loop, the CPU savings could be significant.)

Comments

No comments yet. Leave a Comment

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss

Community Forum

Explore Visual Basic

By Category

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.