Tools Links Login

Quick String Parsing of a Comma Delimited String

This example will show you how parse a list a variables quickly with minimal coding.

Original Author: Mark Collins

Inputs

A string of variables separated by a common like "stuff1,stuff2,stuff3"

Returns

Returns individual variables

Code

Sub TestMe()
  'Run this sub to demonstate how the parsing functions work.
  'This is an example to show begining vb programmers how to parse strings
  'quickly with out having to use mid/instr/right/left every time then need
  'the next item in a string of items.
  
  'Declare our working strings
  Dim myString As String
  Dim curShiz As String, curVar As String
  
  'Declare our name containers
  Dim Name1 As String
  Dim Name2 As String, Name3 As String, Name4 As String
  
  myString = "Tom,Debbie,Mark,Joanie"
  
  'Get tom
  curVar = GetFirstVar(myString)
  myString = GetRestOfVars(myString)
  Name1 = curVar
  
  'Get Debbie
  curVar = GetFirstVar(myString)
  myString = GetRestOfVars(myString)
  Name2 = curVar
  
  'Get MArk
  curVar = GetFirstVar(myString)
  myString = GetRestOfVars(myString)
  Name3 = curVar
  
  'Get joanie
  curVar = GetFirstVar(myString)
  myString = GetRestOfVars(myString)
  Name4 = curVar
  
  MsgBox "Name1 = " & Name1
  MsgBox "Name2 = " & Name2
  MsgBox "Name3 = " & Name3
  MsgBox "Name4 = " & Name4
End Sub
Public Function GetFirstVar(sStr As String)
  'Given a string like: "choad,flap,blah"
  'this returns "choad"
  f = InStr(1, sStr, ",")
  If f = 0 Then
    GetFirstVar = sStr
  Else
    GetFirstVar = Left(sStr, f - 1)
  End If
End Function
Public Function GetRestOfVars(sStr As String)
  'Given a string like: "choad,flap,blah"
  'this returns "flap,blah"
  f = InStr(1, sStr, ",")
  GetRestOfVars = Right(sStr, Len(sStr) - f)
  
End Function

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 93 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.