|
-
Nov 2nd, 2000, 03:15 PM
#1
Thread Starter
Fanatic Member
Recently someone asked me about using Split, and neither I nor this person had VB6, so I decided to make up something of my own that does the same thing. I'll show my function below for any who might be interested (really only applies to VB5 or lower users). Also, is this similar to how Split really operates in VB6? If anyone has any ideas for improvements, or wants to point out my sloppiness , please let me know. This is just a working version. One problem I know of already is that it won't handle the token delimiter appearing consecutively. Like if you have 2 or 3 spaces between words, and use a space as the token delimiter. If there are an even number of spaces, one will be tacked onto the next token found. For odd numbers of spaces, a space appears as a token itself.
Code:
Public Function Split(strString As String, strTokenDelimiter As String, strTokens() As String) As Long
Dim lngCurPos As Long
Dim lngLastPos As Long
Dim lngNumTokens As Long
If (strString = "") Or (strTokenDelimiter = "") Then
Split = 0
Exit Function
End If
If InStr(1, strString, strTokenDelimiter, vbTextCompare) = 0 Then
lngNumTokens = 1
ReDim strTokens(lngNumTokens) As String
strTokens(lngNumTokens - 1) = strString
Split = lngNumTokens
Exit Function
End If
lngLastPos = 1
lngCurPos = InStr(lngLastPos, strString, strTokenDelimiter, vbTextCompare)
lngNumTokens = 0
Do
lngNumTokens = lngNumTokens + 1
ReDim Preserve strTokens(lngNumTokens) As String
strTokens(lngNumTokens - 1) = Mid$(strString, lngLastPos, lngCurPos - lngLastPos)
lngLastPos = lngCurPos + 1
lngCurPos = InStr(lngLastPos + 1, strString, strTokenDelimiter, vbTextCompare)
Loop Until lngCurPos = 0
lngNumTokens = lngNumTokens + 1
ReDim Preserve strTokens(lngNumTokens) As String
strTokens(lngNumTokens - 1) = Mid$(strString, lngLastPos)
Split = lngNumTokens
End Function
Basically, you pass along a string to parse, the token delimiter of the string, and an array to hold the tokens (if found). The return value is the number of total tokens found, or 0 if there were none. I set it up so it always finds 1 token (that token being the original string itself) as long as both the string to parse and the token delimiter are valid strings themselves. Otherwise, it will split up the string and stick the tokens into the array. The array will go from 0 to lngNumTokens - 1 (I start my arrays at 0 as a result of old C habits )
[Edited by Kaverin on 11-02-2000 at 03:19 PM]
I'm baaaack...
VB5 Professional Edition, VC++ 6
Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se
I feel special because I finally figured out how to loop midis: Post link
I'm a fanatic too 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|