Retrieve Field Values in a Delimited String (or Words in a String)

Category:
String Manipulation
Type:
Snippets
Difficulty:
Beginning
Author:
Gavin Bollard

Version Compatibility: Visual Basic 6, Visual Basic 5

More information:
This routine was written when I was handling a series of comma delimited files (of varying sizes). I used the vb line input statement to read a line from the file into memory, and needed to be able to extract a single field.

This is an especially useful function for VB5, since you can implement similar functionality in VB6 with the split function. For example - the following string contains four fields. If I specified the delimiter as a comma and asked for field 2 I should get 02 9805 9809. Roger Wilco, 02 9805 9809, Space Fleet Headquarters, Janitor Since sentances are delimited by spaces, this routine could also be used to extract a particular words in a string (by passing a single space as a delimiter).

Instructions: Copy the declarations and code below and paste directly into your VB project.

Declarations:

Code: