by DoctorMO & Dave Edwards (2 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Originally Published: Fri 22nd June 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Version 2, of My Advanced String Function now with added Assembler ideals and notes. This Function Replaces the Standard InStr (VB 5+) and
API Declarations
' Replacement function for InStr() (VB 1.0 onwards) and InStrRev()
' VB 6 onwards).
' Synopsis:
' InStrEx(StartPos As Long, SourceString As String,
' SearchString As String,
' Optional IS_Mode As Long = 1,
' Optional OrdSelector As Integer) As Long
'
' StartPos is the character position from where the search
' begins (1=first character of SourceString, etc)
' If omitted (or equals zero), then function intelligently
' coerces this value to a sensible one.
' SourceString is the string within which the search is performed.
' SearchString is the string being sarched for.
' IS_Mode : range of flags specified by the following constants:
' IS_Mode_For : specifies forward search through the SourceString
' (StarPos default = 1)
' IS_Mode_Back : specifies backward search throuhgh the
' SourceString (StartPos default = length
' of SourceString)
' IS_Mode_Number : Specifies that the search is to find the
' NUMBER of occurrences of SearchString in
' SourceString, starting from the given
' start position, and searching in the
' relevant direction.
' IS_Mode_NoCase : if added to any combination of the above,
' specifies that the search is to be case
' INSENSITIVE (default is case sensitive).
'
' These constants can be added (bitwise) to combine modes, e.g.,
' IS_Mode_Back + IS_Mode_Number will search backwards from the
' specified search starting position, and count how many
' occurrences of SearchString it has found in SourceString.
' If no further occurrences of SearchString are found from the
' chosen search point, then InStrEx() returns zero (naturally
' enough).
' OrdSelector is the Ordinal Selector, i.e., specifies WHICH
' occurrence of SearchString is to be searched for (not valid
' in conjunction with IS_Mode_Number).
Public Const IS_Mode_For = 0
Public Const IS_Mode_Back = 1
Public Const IS_Mode_Number = 2
Public Const IS_Mode_NoCase = 4
Public Function InStrEx(StartPos As Long, SourceString As String, SearchString As String, Optional IS_Mode As Long = 1, Optional OrdSelector As Integer) As Long
Dim Z As Long
Dim CN As Long, ChrSteping As Integer
Dim ChrFrom As Long, ChrTo As Long
Dim tmpString1 As String, tmpString2 As String
If SourceString = "" Then Exit Function
tmpString1 = SourceString
tmpString2 = SearchString
InStrEx = 0
'This conditional test checks to see if bit 2 of IS_Mode (corresponding to
' a value of 4) is set. If it is, then we've selected no case sensitivity.
If (IS_Mode And IS_Mode_NoCase) <> 0 Then
tmpString1 = UCase(tmpString1)
tmpString2 = UCase(tmpString2)
End If
'This is a check to see if user has entered a nonsensical value for the
'ordinal selector. If so, set it to a default value of 1 initially,
'then correct the value because, for the first search, it actually needs
'to be zero (and so on for the other values ... N-1)
CN = OrdSelector
If CN = Null Or CN = 0 Then CN = 1
CN = CN - 1
'This select checks the lowest bit position only (i.e., bit 0). If it's set,
'then we're searching backwards, otherwise we're searching forwards.
Select Case (IS_Mode And &H1)
Case IS_Mode_For
ChrSteping = 1
If StartPos = 0 Or StartPos = vbNull Then StartPos = 1
ChrFrom = StartPos
ChrTo = Len(tmpString1)
Case IS_Mode_Back
ChrSteping = -1
If StartPos = 0 Or StartPos = vbNull Then StartPos = Len(tmpString1)
ChrFrom = StartPos
ChrTo = 1
End Select
'Now actually start searching for our string!
For Z = ChrFrom To ChrTo Step ChrSteping
Str1 = Mid(tmpString1, Z, Len(tmpString2))
If Str1 = tmpString2 Then
'Again, a bit position test - this time, to test bit position 1, to see if
'we're searching for the Nth occurrence of SearchString in SourceString.
If (IS_Mode And IS_Mode_Number) <> 0 Then
InStrEx = InStrEx + 1
Else
InStrEx = Z
If CN = 0 Then
Exit Function
Else
CN = CN - 1
End If
End If
End If
Next Z
'If we're simply performing a forward or backward search, and we reach
'the end of the loop, then we've scanned the entire SourceString, and not
'found the specified occurrence of the SearchString. So, we need to signal
'this fact by returning the special value -1.
If (IS_Mode And IS_Mode_Number) = 0 Then
InStrEx = -1
End If
End Function
No comments have been posted about Version 2, of My Advanced String Function now with added Assembler ideals and notes. This Function . Why not be the first to post a comment about Version 2, of My Advanced String Function now with added Assembler ideals and notes. This Function .