Count occurrences of a substring: Difference between revisions
Content deleted Content added
No edit summary |
|||
Line 1,965: | Line 1,965: | ||
2 |
2 |
||
</lang> |
</lang> |
||
=={{header|Red}}== |
|||
<lang Red>Red [] |
|||
;;----------------------------------- |
|||
count-sub1: func [hay needle][ |
|||
;;----------------------------------- |
|||
prin rejoin ["hay: " pad copy hay 20 ",needle: " pad copy needle 6 ",count: " ] |
|||
i: 0 |
|||
parse hay [ some [thru needle (i: i + 1)] ] |
|||
print i |
|||
] |
|||
;;----------------------------------- |
|||
count-sub2: func [hay needle][ |
|||
;;----------------------------------- |
|||
prin rejoin ["hay: " pad copy hay 20 ",needle: " pad copy needle 6 ",count: " ] |
|||
i: 0 |
|||
while [hay: find hay needle][ |
|||
i: i + 1 |
|||
hay: skip hay length? needle |
|||
] |
|||
print i |
|||
] |
|||
count-sub1 "the three truths" "th" |
|||
count-sub1 "ababababab" "abab" |
|||
print "^/version 2" |
|||
count-sub2 "the three truths" "th" |
|||
count-sub2 "ababababab" "abab" |
|||
</lang> |
|||
{{out}} |
|||
<pre>hay: the three truths ,needle: th ,count: 3 |
|||
hay: ababababab ,needle: abab ,count: 2 |
|||
version 2 |
|||
hay: the three truths ,needle: th ,count: 3 |
|||
hay: ababababab ,needle: abab ,count: 2 |
|||
>> </pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |