Jump to content

Count occurrences of a substring: Difference between revisions

My first ever edit in an Ubuntu Virtual Machine
(Add Batch File)
(My first ever edit in an Ubuntu Virtual Machine)
Line 1,827:
2 occurence(s) of cat inside catapultcatalog</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bash}}
<lang bash>#!/bin/bash
 
function countString(){
input=$1
cnt=0
 
until [ "${input/$2/}" == "$input" ]; do
input=${input/$2/}
let cnt+=1
done
echo $cnt
 
}
 
countString "the three truths" "th"
countString "ababababab" "abab"</lang>
{{Out}}
<pre>3
2</pre>
 
=={{header|VBA}}==
535

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.