Solved

strtok function

Posted on 1998-07-09
8
1,014 Views
Last Modified: 2010-05-18
I would like a function to do the same as the strtok function under C.  It should take a variable number of tokens (characters, could all be in one string just like strtok) to tokenize on.   Each token should be a string.

eg:

' Tokenize based on command ',' and space ' '
strtok("Have      a  nice,     day", " ,")

This should break the above string into :
"Have"
"a"
"nice"
"day"

Thanks for any help
Guava
0
Comment
Question by:guava070998
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 6

Accepted Solution

by:
anthonyc earned 80 total points
ID: 1465345
'************************************************************
'Date:    06/08/1998
'Author:  Anthony Cornetto
'Input:
'  strResult()<String>:  Array where results are returned
'  strWorking<String>:   String to parse
'  strDelimiter<String>: Delimiter
'Output:
'  <None>
'Purpose: Goes through string, separating it by the delimiter, and
'         Returning the elements in an array.
'************************************************************
Public Sub SeparateString(strResult() As String, strWorking As String, strDelimiter As String)
    'The separated strings are placed into an array.
   
    Dim lngLastLoc As Long
    Dim lngDelimLoc As Long
    Dim lngFieldCount As Long
    Dim intI As Integer
   
    'clean out string
    ReDim strResult(0 To 0) As String
   
    If strWorking <> "" Then
        'initialize last location to 0
        lngLastLoc = 1
        lngFieldCount = 0
        lngDelimLoc = InStr(lngLastLoc, strWorking, strDelimiter)
       
        'Continue until no more delimiters are found
        Do Until lngDelimLoc = 0
            'Set array size
            If lngFieldCount > 0 Then
                ReDim Preserve strResult(0 To lngFieldCount) As String
            End If
           
            'Set string and locations to find next delim
            strResult(lngFieldCount) = Mid$(strWorking, lngLastLoc, lngDelimLoc - lngLastLoc)
            lngLastLoc = lngDelimLoc + Len(strDelimiter)
            lngDelimLoc = InStr(lngLastLoc, strWorking, strDelimiter)
            lngFieldCount = lngFieldCount + 1
        Loop
       
        'add last field (after delim)
        If lngFieldCount > 0 Then
            ReDim Preserve strResult(0 To lngFieldCount) As String
        End If
        strResult(lngFieldCount) = Mid$(strWorking, lngLastLoc)
    End If
End Sub

0
 
LVL 4

Expert Comment

by:tomook
ID: 1465346
anthonyc, that really does not mimic strtok. Here is one that does, including using static variables.

' Tokenize a string like 'strtok' in C std lib
' Does not modify source string like strtok.
Function StrTok(Seps As String, Optional SourceString As Variant) As String
    Static LocalString As String
    Static LocalCounter As Long     ' Substitutes for pointer into string
    Dim lCnt1 As Long

    If Not (IsMissing(SourceString)) Then
        LocalString = SourceString
        LocalCounter = 1    ' Must start at first char.
    End If
   
    ' If we start on a separator, skip all seps found.
    While (LocalCounter <= Len(LocalString)) And (InStr(Seps, Mid$(LocalString, LocalCounter, 1))) <> 0
        LocalCounter = LocalCounter + 1
    Wend
   
    '
    lCnt1 = LocalCounter
   
    ' Count until separators found.
    While (lCnt1 <= Len(LocalString)) And (InStr(Seps, Mid$(LocalString, lCnt1, 1)) = 0)
        lCnt1 = lCnt1 + 1
    Wend
       
    StrTok = Mid$(LocalString, LocalCounter, lCnt1 - LocalCounter)
   
    ' LocalCounter must point to next char to examine
    LocalCounter = lCnt1 + 1
End Function

0
 
LVL 6

Expert Comment

by:anthonyc
ID: 1465347
Yeah, you are right.. It's an enhanced version.  You get all of the tokens returned in 1 pass, in 1 array.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 4

Expert Comment

by:tomook
ID: 1465348
Yeah, I noticed that. I guess it depends on what you are looking for. In writing VB parsers, I usually need to change the separators so I use the version I posted.
0
 

Author Comment

by:guava070998
ID: 1465349
Hi, thanks for the quick replies.  I will try this out as soon as possible and get back to you both on if either suits what I happen to need.

Thanks again,
guava
0
 

Author Comment

by:guava070998
ID: 1465350
Hi, I tried both, both work, and I like both....so, what do we do about the points here?  Technically the second example mimics strtok more, which I asked for...but the first code actually require me to do a wee less work....

How would you both feel about getting this question archived, my points returned, and I ask two 40 point questions, one for each of you, just to pick up the points? (ie: you would just have to offically answer the question, now actual work involved since you answered this one).

Does that sound good, or should I just decide which code does my job better and answer?

guava
0
 
LVL 6

Expert Comment

by:anthonyc
ID: 1465351
guava,

Anything is fine with me
0
 
LVL 4

Expert Comment

by:tomook
ID: 1465352
I am easy.
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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

623 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