Count occurrences of a substring: Difference between revisions

Line 1,475:
=={{header|MiniScript}}==
<lang MiniScript>// Count occurrences of a Substring
 
string = "the three truths"
countSubstring = function(string, substring)
print string.split("th").len - 1
count = 0
string = "ababababab"
place = null
print string.split("abab").len - 1</lang>
processing = true
while processing
found = string.indexOf(substring,place)
if found == null then
processing = false
else
count = count + 1
place = found + substring.len - 1
end if
end while
return count
end function
print countSubstring("the three truths","th")
print countSubstring("ababababab","abab")</lang>
{{out}}
<pre>