Check if text contains substring


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'**********************************************************************************************************
' Name:                 contains
' Author:               mielk | 2014-09-20
'
' Description:          Function to check if the given string contains substring given as a second
'                       parameter.
'
' Parameters:
'   baseString          Base string that will be searched.
'   lookFor             Substring the function is looking for in the base string.
'   isCaseSensitive     Optional parameter of Boolean type.
'                       It determines if text searching is case sensitive.
'                       * If this value is set to True, searching is case sensitive - a letter in lowercase
'                         is treated as different than the same letter in uppercase (i.e. a ? A).
'                       * If this value is set to False, it doesn't matter if a letter is in lowercase or
'                         in uppercase, since both of them are considered as the same character
'                         (i.e. a = A).
'                       * Default value of this parameter is True.
'
'
' Returns:
'   Boolean             True - if the given substring is contained in the specified base text.
'                       False - otherwise.
'
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2014-09-20        mielk       Function created.
'**********************************************************************************************************
Public Function contains(baseString As String, lookFor As String, _
                                                    Optional isCaseSensitive As Boolean = False) As Boolean
    Const METHOD_NAME As String = "contains"
    '------------------------------------------------------------------------------------------------------
    Dim uCompareMethod As VBA.VbCompareMethod
    '------------------------------------------------------------------------------------------------------


    'Convert [isCaseSensitive] parameter of Boolean type to the [VbCompareMethod] enumeration. ----------|
    If isCaseSensitive Then                                                                             '|
        uCompareMethod = VBA.vbBinaryCompare                                                            '|
    Else                                                                                                '|
        uCompareMethod = VBA.vbTextCompare                                                              '|
    End If                                                                                              '|
    '----------------------------------------------------------------------------------------------------|


    'Check if substring is contained in base string by using VBA built-in function VBA.InStr. -----------|
    contains = (VBA.InStr(1, baseString, lookFor, uCompareMethod) > 0)                                  '|
    '----------------------------------------------------------------------------------------------------|


End Function