String matching: Difference between revisions

m (Add Julia 1.0 version)
Line 3,114:
first line is a suffix of the second line</pre>
 
=={{header|VBA}}==
{{trans|Phix}}<lang vb>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</lang>{{out}}
<pre>yes(1)
yes(2)
yes(3)
yes(4)
1
16
33 </pre>
=={{header|VBScript}}==
<lang vb>Function StartsWith(s1,s2)
255

edits