T
Tom Ellison
I received a request for this function, which I had offered to provide in an
earlier post here. Rather than send it just to the person who emailed me,
I'm providing it here.
Call the function with 3 parameters, none are optional. The first is a
string to be searched. The second is a string (often just one character, or
a CrLf) to search for, which I think of as the parse delimiter. The third
is an integer showing which "substring" to return.
Everything from the start of the string being searched, up to the string
that parses it, is the first parsing. Starting after that first occurance
of the delimit string is the second parsing, ending at the next occurance of
the delimit string or the end of the string.
The function is case sensitive.
For example:
"Now is the time for all good men to come to the aid of their country"
Parse this using "i"
1 -5 Now
2 -4 s the t
3 -3 me for all good men to come to the a
4 -2 d of the
5 -1 r country
What I've shown is the 5 substrings of the phrase being parsed, broken at
each occurrance of "i". They are numbered to the left from 1 to 5. If your
3rd parameter is 1 to 5 you'll get the string shown.
The funcion will also parse from the end of the string, using a negative
value for the 3rd parameter Occurrance. So, -1 is the last occurrance, -2
is the next to last, and so on.
It returns an empty string if the occurrance of the delimited string does
not exist.
Public Function Parse1(ByVal ObjectString As String, ByVal DelimitString As
String, ByVal Occurrance As Integer) As String
Dim Count As Integer, Begin As Integer, Found As Integer, DelimitLength
As Integer
DelimitLength = Len(DelimitString)
Parse1 = ""
If Occurrance < 0 Then
Count = 1
Begin = 1
Do
Found = InStr(Begin, ObjectString, DelimitString)
If Found = 0 Then Exit Do
Begin = Found + DelimitLength
Count = Count + 1
Loop
Occurrance = Count + Occurrance + 1
End If
Count = 1
Begin = 1
Do While Count < Occurrance
Found = InStr(Begin, ObjectString, DelimitString)
If Found = 0 Then Exit Function
Begin = Found + DelimitLength
Count = Count + 1
Loop
Found = InStr(Begin, ObjectString, DelimitString)
If Found = 0 Then Found = Len(ObjectString) + 1
Parse1 = Mid(ObjectString, Begin, Found - Begin)
End Function
Enjoy!!! (this line is not part of the code!)
Tom Ellison
earlier post here. Rather than send it just to the person who emailed me,
I'm providing it here.
Call the function with 3 parameters, none are optional. The first is a
string to be searched. The second is a string (often just one character, or
a CrLf) to search for, which I think of as the parse delimiter. The third
is an integer showing which "substring" to return.
Everything from the start of the string being searched, up to the string
that parses it, is the first parsing. Starting after that first occurance
of the delimit string is the second parsing, ending at the next occurance of
the delimit string or the end of the string.
The function is case sensitive.
For example:
"Now is the time for all good men to come to the aid of their country"
Parse this using "i"
1 -5 Now
2 -4 s the t
3 -3 me for all good men to come to the a
4 -2 d of the
5 -1 r country
What I've shown is the 5 substrings of the phrase being parsed, broken at
each occurrance of "i". They are numbered to the left from 1 to 5. If your
3rd parameter is 1 to 5 you'll get the string shown.
The funcion will also parse from the end of the string, using a negative
value for the 3rd parameter Occurrance. So, -1 is the last occurrance, -2
is the next to last, and so on.
It returns an empty string if the occurrance of the delimited string does
not exist.
Public Function Parse1(ByVal ObjectString As String, ByVal DelimitString As
String, ByVal Occurrance As Integer) As String
Dim Count As Integer, Begin As Integer, Found As Integer, DelimitLength
As Integer
DelimitLength = Len(DelimitString)
Parse1 = ""
If Occurrance < 0 Then
Count = 1
Begin = 1
Do
Found = InStr(Begin, ObjectString, DelimitString)
If Found = 0 Then Exit Do
Begin = Found + DelimitLength
Count = Count + 1
Loop
Occurrance = Count + Occurrance + 1
End If
Count = 1
Begin = 1
Do While Count < Occurrance
Found = InStr(Begin, ObjectString, DelimitString)
If Found = 0 Then Exit Function
Begin = Found + DelimitLength
Count = Count + 1
Loop
Found = InStr(Begin, ObjectString, DelimitString)
If Found = 0 Then Found = Len(ObjectString) + 1
Parse1 = Mid(ObjectString, Begin, Found - Begin)
End Function
Enjoy!!! (this line is not part of the code!)
Tom Ellison