VB icon

_InStrLike

Email
Submitted on: 1/8/2015 5:39:00 PM
By: Atul Brad Buono (from psc cd)  
Level: Intermediate
User Rating: By 8 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 2331
 
     This is a combination of InStr and the Like operator. It returns the position of a mask within a string. The parameters are all user friendly variants just like the regular InStr function. Example: InStrLike("Test String 123abc45 Stuff","###*##") returns 13, because 123abc45 matches the mask and it starts at character 13. Hope this is useful to somebody.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: _InStrLike
' Description:This is a combination of InStr and the Like operator. It returns the position of a mask within a string. The parameters are all user friendly variants just like the regular InStr function.
Example:
InStrLike("Test String 123abc45 Stuff","###*##")
returns 13, because 123abc45 matches the mask and it starts at character 13. Hope this is useful to somebody.
' By: Atul Brad Buono (from psc cd)
'
' Inputs:Start=Position to start searching, optional
String1=String to search
String2=Mask to search for
intCompareMethod=vbCompareMethod to use, optional
'
' Returns:Returns a variant, which is null if String1 or String2 is null, otherwise returns the position of the mask (String2) within the string (String1). 0 if the mask is not present.
'
' Assumes:This currently does not account for searching for the literal mask characters, which are normally enclosed in brackets in the mask. If that doesn't make sense to you then you are probably not doing it anyways so don't worry about it.
'**************************************

Public Function InStrLike(Optional ByVal Start, Optional ByVal String1, Optional ByVal String2, Optional ByVal intCompareMethod As VbCompareMethod = vbTextCompare) As Variant
On Error GoTo err_InStrLike
 Dim intPos As Integer
 Dim intLength As Integer
 Dim strBuffer As String
 Dim blnFound As Boolean
 Dim varReturn As Variant
 If Not IsNumeric(Start) And IsMissing(String2) Then
 String2 = String1
 String1 = Start
 Start = 1
 End If
 If IsNull(String1) Or IsNull(String2) Then
 varReturn = Null
 GoTo exit_InStrLike
 End If
 If Left(String2, 1) = "*" Then
 err.Raise vbObjectError + 2600, "InStrLike", "Comparison mask cannot start with '*' since a start position cannot be determined."
 Exit Function
 End If
 For intPos = Start To Len(String1) - Len(String2) + 1
 If InStr(1, String2, "*", vbTextCompare) Then
For intLength = 1 To Len(String1) - intPos + 1
strBuffer = Mid(String1, intPos, intLength)
If strBuffer Like String2 Then
 blnFound = True
 GoTo done
End If
Next intLength
 Else
strBuffer = Mid(String1, intPos, Len(String2))
If strBuffer Like String2 Then
blnFound = True
GoTo done
End If
 End If
 Next intPos
done:
 
 If blnFound = False Then
 varReturn = 0
 Else
 varReturn = intPos
 End If
exit_InStrLike:
 InStrLike = varReturn
 Exit Function
err_InStrLike:
 Select Case err.Number
 Case Else
varReturn = Null
MsgBox err.Description, vbCritical, "Error #" & err.Number & " (InStrLike)"
GoTo exit_InStrLike
 End Select
End Function


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.