Advertisement
5_2007-2008 String Manipulation #177502

Quick String Parsing of a Comma Delimited String

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

AI

Riepilogo AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Codice sorgente
original-source
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
Commenti originali (3)
Recuperato da Wayback Machine