Starts with ...


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
'**********************************************************************************************************
' Name:                 startsWith
' Author:               mielk | 2012-06-21
'
' Comment:              Checks if the given string starts with the specified prefix.
'
' Parameters:
'   str                 String to be checked.
'   prefix              The prefix
'   isCaseSensitive     Optional parameter of Boolean type.
'                       It determines if text matching 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 string [str] starts with the given prefix.
'                       False - otherwise.
'
'
' --- Changes log -----------------------------------------------------------------------------------------
' 2012-06-21        mielk           Function created.
'**********************************************************************************************************
Public Function startsWith(str As String, prefix As String, Optional isCaseSensitive As Boolean = True) _
                                                                                                 As Boolean
    Const METHOD_NAME As String = "startsWith"
    '------------------------------------------------------------------------------------------------------
    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                                                                                              '|
    '----------------------------------------------------------------------------------------------------|


    '----------------------------------------------------------------------------------------------------|
    If VBA.StrComp(VBA.Left$(str, VBA.Len(prefix)), prefix, uCompareMethod) = 0 Then                    '|
        startsWith = True                                                                               '|
    End If                                                                                              '|
    '----------------------------------------------------------------------------------------------------|


End Function