Solved

Parse string function

Posted on 1998-10-12
3
164 Views
Last Modified: 2010-04-30
I have 2 strings. In these strings I have values seperated by ;

Locations = "blablubla;bobbabbib;jamjam;namnam;"
Visits = "200;300;50;80;"

I need a function that can give me the X value of both strings.
Example:
strValue1 = Gime(Locations,2)
strValue2 = Gime(Visits,2)
will return this:
strValue1 = bobbabbib
strValue2 = 300
0
Comment
Question by:jfl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
clifABB earned 20 total points
ID: 1439498
Public Function ParseString(sText as String, nSection as Integer)
  Dim sTemp   As String
  Dim nPtr    As Integer
  Dim nCnt1   As Integer

  For nCnt1 = 1 To nSection
    nPtr = Instr(nPtr + 1, sText, ";")
    If nPtr = 0 Then
      ParseString = ""
      Exit Function
    End If
  Next nCnt1
  sTemp = Mid$(sText, nPtr + 1)
  ParseString = Left$(sTemp, Len(sTemp) - Instr(sTemp, ";")
End Function
0
 
LVL 6

Expert Comment

by:clifABB
ID: 1439499
Oops...
I tried to do this off the top of my head, but screwed up.

Here is the real working code (I tested it this time):

Public Function ParseString(sText As String, nSection As Integer)
  Dim sTemp   As String
  Dim nPtr    As Integer
  Dim nCnt1   As Integer

  For nCnt1 = 1 To nSection - 1
    nPtr = InStr(nPtr + 1, sText, ";")
    If nPtr = 0 Or nPtr >= Len(sText) Then
      ParseString = ""
      Exit Function
    End If
  Next nCnt1
  sTemp = Mid$(sText, nPtr + 1)
  nPtr = InStr(sTemp, ";") - 1
  ParseString = Left$(sTemp, nPtr)
End Function

0
 
LVL 4

Expert Comment

by:mcix
ID: 1439500
Here's a little function that does the same sort of thing:

Function Token(LookIn As String, Delim As String, tkno As Integer) As String

    ' this function extracts token #tkno from a
    ' string of tokens (LookIn) separated by a delimiter (Delim).
    ' last character in (LookIn) *must* be a delimiter

    Dim ptr As Long, ptr0 As Long, tptr As Long
    ptr = 1
    ptr0 = 1
    tptr = 1
   
    Do
        ptr = InStr(ptr0, LookIn, Delim)

        ' if we can't find any more tokens
        ' check to see if this is the one we want
        If ptr = 0 Then
            If tptr = tkno Then
                Token = Mid(LookIn, ptr0)
            Else
                Token = Empty
                Exit Do
            End If
        Else
            ' do we have correct token?
            If tptr = tkno Then
           
                Token = Mid(LookIn, ptr0, ptr - ptr0)
                Exit Do
            End If
        End If
        tptr = tptr + 1
        ptr0 = ptr + 1
    Loop
End Function


0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question