Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
31
Task/String-matching/VBA/string-matching.vba
Normal file
31
Task/String-matching/VBA/string-matching.vba
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
Public Sub string_matching()
|
||||
word = "the" '-- (also try this with "th"/"he")
|
||||
sentence = "the last thing the man said was the"
|
||||
'-- sentence = "thelastthingthemansaidwasthe" '-- (practically the same results)
|
||||
|
||||
'-- A common, but potentially inefficient idiom for checking for a substring at the start is:
|
||||
If InStr(1, sentence, word) = 1 Then
|
||||
Debug.Print "yes(1)"
|
||||
End If
|
||||
'-- A more efficient method is to test the appropriate slice
|
||||
If Len(sentence) >= Len(word) _
|
||||
And Mid(sentence, 1, Len(word)) = word Then
|
||||
Debug.Print "yes(2)"
|
||||
End If
|
||||
'-- Which is almost identical to checking for a word at the end
|
||||
If Len(sentence) >= Len(word) _
|
||||
And Mid(sentence, Len(sentence) - Len(word) + 1, Len(word)) = word Then
|
||||
Debug.Print "yes(3)"
|
||||
End If
|
||||
'-- Or sometimes you will see this, a tiny bit more efficient:
|
||||
If Len(sentence) >= Len(word) _
|
||||
And InStr(Len(sentence) - Len(word) + 1, sentence, word) Then
|
||||
Debug.Print "yes(4)"
|
||||
End If
|
||||
'-- Finding all occurences is a snap:
|
||||
r = InStr(1, sentence, word)
|
||||
Do While r <> 0
|
||||
Debug.Print r
|
||||
r = InStr(r + 1, sentence, word)
|
||||
Loop
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue