Count occurrences of a substring: Difference between revisions
Content added Content deleted
(→{{header|BQN}}: Scan-based solution) |
|||
Line 3,200: | Line 3,200: | ||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
'''user created function''' |
|||
<lang vb> |
<lang vb> |
||
Function CountSubstring(str,substr) |
Function CountSubstring(str,substr) |
||
Line 3,218: | Line 3,219: | ||
WScript.StdOut.Write CountSubstring("ababababab","abab") & vbCrLf |
WScript.StdOut.Write CountSubstring("ababababab","abab") & vbCrLf |
||
</lang> |
</lang> |
||
'''Using built-in Regexp''' |
|||
Run it with CScript. |
|||
<lang vb> |
|||
function CountSubstring(str,substr) |
|||
with new regexp |
|||
.pattern=substr |
|||
.global=true |
|||
set m=.execute(str) |
|||
end with |
|||
CountSubstring =m.count |
|||
end function |
|||
WScript.StdOut.Writeline CountSubstring("the three truths","th") |
|||
WScript.StdOut.Writeline CountSubstring("ababababab","abab") |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |