Programmer's Notebook

VBScript Split Function

The VBScript Split function takes a string and returns it in an array broken up by the delimiter of your choosing. The basic usage is: somearray = split(expression), where expression is the string you need to parse. Since split defaults to using a blank space as a delimiter, in its basic form you could use split to divide a sentence into individual words. Note that somearray lacks parentheses both when calling the function and when declaring the variable. It is by its nature a one-dimensional array with a number of elements equal to the number of items return from the parse.

Here is a sample which splits a sentence into individual words and then echoes them back in a message box just to verify that the parse took place. Also, to format the output a little and further confirm that each word became a separate variable, I used the constant vbCRLF to add a carriage return / line feed between each word in the echo. (The CR LF in text files is usually inserted by the user hitting the "enter" key. If you are typing in Notepad and hit "enter," the invisible two characters generated put a hard break to your line and wrap you around to the next.)

Example 1

You can run this sample yourself by pasting the above code into Windows Notepad and saving the file with a *.vbs extenstion (i.e. sample.vbs).

If you do run the sample you'll notice that the last word parsed still has a period attached. Had this been a real world coding problem we would certainly have to deal with that period - but for now we'll just let it ride.

The full syntax for split is: Split(expression[,delimiter[,count[,compare]]])

You can use the optional delimiter parameter to specify a delimiter other than a blank space. One of the most common delimiters would be a comma, as used in the ubiquitous comma separated value (csv) format. Such csv files are flat file databases in which fields are separated by commas and records are separated by carriage return / line feeds.

While it's not exactly a csv parser, in the following example I use split to parse a line of text delimited by commas rather than blank spaces.

Example 2

The only coding difference between the first and second examples is the addition of the delimiter parameter "," .

The remaining optional parameters are count and compare. Count returns the number of substrings to be returned. The default of -1 returns every substring. Compare gives you the option of doing either a binary or case-insensitive textual comparison. In short, if you want "ABC" to be equal to "abc" then use 1, if you want "ABC" and "abc" not to be equal then use 0.

See more from Web Design


Other Pages You Might Enjoy...

The Commodore VIC-20 - The Friendly Computer
Released in 1981, the Commodore VIC-20 was the best selling computer of its day. Inexpensive and user-friendly, the computer opened up the market to a new group of consumers. It was the first computer many families owned, providing the earliest exposure to computing for countless future programmers, engineers and entrepreneurs around the world.

Grace Hopper - Matriarch of Programming
Grace Murray Hopper enjoyed one of the most storied careers in computing history. From the earliest days working on the Harvard Mark I, her insights and innovations helped establish the foundations for modern, user friendly computers and launched the information age.

The Meissen Porcelain Manufactory
For more than 300 years the Meissen Porcelain Manufactory has been a central figure in European porcelain. It was at Meissen that the first true hard-paste porcelain was manufactured outside of China, and the story of that accomplishment has the high drama of fiction - all the more intriguing because it is true.

Pirated Glassware that Collectors Want to Own
The 1996 discovery of what is believed to be the wreckage of Blackbeard's ship Queen Anne's Revenge, is providing historians and collectors new information about the fragile and transparent objects that came onto the North American Continent before arrival of the first piece of cut glass.

Collecting Antique Furniture
Of all the collectible antiques, the broad category of furniture is perhaps the most widely held and most satisfying. Frequently handed down through generations, the various chairs, tables, cabinets and other pieces are often in use, adding character and constancy to our daily lives. This guide includes an overview of the significant styles and historical periods, and other important information for collectors.

Collecting Antique Clocks and Watches
Information for collectors of antique clocks and watches. Includes a history of clock and watchmaking, an overview of styles, and important terminology.

Collecting Antique Jewelry
Embodying the wealth and treasure of bygone days, the antique jewelry pieces available today include some of the world's most valuable artifacts. Includes a history of jewelry making, an overview of the major historical periods, and a glossary of antique jewelry terminology.

Javascript Chatterbot Workshop
Home of the Do It Yourself Chatterbot Project. Has a ready to go chatterbot shell and full instructions on how to program it.

An Unorthodox Javascript Form Submission
Describes three fixes in handling an unorthodox Javascript form submission. Executing Javascript before submitting the data to the server; altering the behavior of the PHP running on the server so that it would return to the original page; and restarting animated GIFs.

Update Your Navigation and Other Content Automatically with JavaScript
Learn to use this simple JavaScript technique to change content and make instant updates across your entire site. Whether you update five pages or thousands, this handy web tip is a must read!

Tips for Improving Website Navigation
A business often has more than one target audience it wants to reach with its website. Different customers mean different sales appeals and a homepage needs to be able to direct visitor traffic to go to the right places to get information or make a sale.

Choosing the Right Color for Your Web Site
When people build a web site for their business (or hire a web design company to make it for them), not much thought is put into deciding what color their web site will be as most would rather focus on more important tasks like creating content, web site optimization and promotion. Most simply choose the most web-friendly colors, others use their company's logo as base while some decide to just use their favorite color.

A Longest Common Substring Function for Javascript
Here is a down and dirty Javascript function that returns the longest substring shared by two string variables.

Beginning at the Beginning - File Systems and Plain Text Files
Getting up and running with HTML, Javascript, and a host of other technologies requires an understanding of file systems and text editing tools. This page is for people who need to come up-to-speed on the basics before moving on to introductory markup and coding.

HTML For the Absolute Beginner
The intent of this tutorial is to provide a basic introduction to making web pages for the person who has never studied HTML before. Once one has worked through this material they should be able to create a bare bones HTML file and be ready to move on to learning to build web pages.



The MiFio Network

Visit cache.MiFio.com for more pages about business, technology, programming, home, travel and more.

Visit www.MiFio.com the website dedicated to antiques and collectibles.