Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSee if a string contains a minimal piece of another string
Keywordssubstring
CategoriesStrings
 
By Bill Roberts.

I had a requirement to match incoming file names to existing file names (to decide which database table to update from them), where both contained a common "root", such as 20021008_TestFile_012.txt and 2002_10_04_Copy_TestFile_Old.bak. Both files have a "root" name of TestFile.

InStr wouldn't work by itself, so this function will match a specified part of the "pattern" string to the "Search" string.

The routine works by examining each substring in sPattern of the minimum length. It uses InStr to see if that substring appears in sSearch. For example, TextFound("just testing","a test file","4",false) looks at substrings of length 4 in the string "testing." It then uses InStr to see if the strings "just", "ust ", "st t", "t te", etc. appear in the string "a test file."

 
' See if piece of sPattern with a minimum length of iMin
' occurs in the string sSearch. If bIgnoreCase is true,
' ignore character case of both strings.
Public Function TextFound(ByVal sPattern As String, ByVal _
    sSearch As String, ByVal iMin As Integer, bIgnoreCase _
    As Boolean) As Boolean
Dim iCount As Integer
Dim sTemp As String

    If bIgnoreCase Then
        sPattern = UCase(sPattern)
        sSearch = UCase(sSearch)
    End If

    For iCount = 1 To Len(sPattern) - iMin + 1
        If InStr(1, sSearch, Mid$(sPattern, iCount, iMin)) _
            > 0 Then
            TextFound = True
            Exit Function
        End If
    Next
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated