Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- C
- Clojure
- C#
- D
- Elixir
- Erlang
- Fortran
- Go
- Haskell
- Haskell
- JS
- Java
- Java
- Java
- Java
- Lisp
- PHP
- Pascal
- Perl
- Python
- Ruby
- Rust
unsigned n;
for (n = 0; s = strstr(s, t); ++n, ++s)
;
Overlapping occurrences are counted.
This destroys the pointer s.
This destroys the pointer s.
int SubstringCount(string s, string t, bool allowOverlap = false)
{
int p = 0;
int tl = allowOverlap ? 1 : t.Length;
int cnt = 0;
while (1 == 1)
{
p = s.IndexOf(t, p);
if (p == -1) break;
p += tl;
cnt++;
}
return cnt;
}
Optional third argument can be set to true to allow overlapping occurences
s |> String.split(t) |> Enum.drop(1) |> length()
countOccurence(List1, List2) ->
countOccurence(List1, List2, 0).
countOccurence(_, [], Count) ->
Count;
countOccurence(List1, [_ | Rest] = List2, Count) ->
case (lists:prefix(List1, List2)) of
true ->
countOccurence(List1, Rest, Count + 1);
false ->
countOccurence(List1, Rest, Count)
end.
countOccurence("ab", "abcddababa").
counts overlapping occurences
lt = len(t) - 1
k = 1
num = 0
do
print *,s(k:)
i = index(s(k:),t)
if (i==0) exit
num = num + 1
k = k + i + lt
end do
print *,num
Overlapping strings will not be counted more than once.
sum [ 1 | r <- tails s, isPrefixOf t r ]
overlapping occurences of t in s are indeed found and counted
length . filter (isPrefixOf t) . tails $ s
Implemented using mostly point free style. Overlapping occurrences are counted.
let n = 0 // the number of occurences
let acc = s
let i
while ((i = acc.indexOf (t)) + 1) {
n++
acc = acc.slice (i + 1)
}
Overlapping occurences are counted.
There's no builtin but at least JavaScript isn't friggin' Pascal.
There's no builtin but at least JavaScript isn't friggin' Pascal.
import static java.util.regex.Pattern.compile;
import static java.util.regex.Pattern.quote;
import java.util.regex.Matcher;
int z = 0;
Matcher m = compile(quote(t)).matcher(s);
while (m.find()) ++z;
import static java.util.regex.Pattern.compile;
import static java.util.regex.Pattern.quote;
import java.util.regex.Matcher;
int z, i = z = 0;
Matcher m = compile(quote(t)).matcher(s);
while (m.find(i)) {
++z;
i = m.start() + 1;
}
This will count overlapped values.
;;; s=str, t=pattern
(defun cnt_substr (str pattern)
(loop for i from 0 to (- (length str) (length pattern) )
sum (if (equal pattern (subseq str i (+ i (length pattern )))) 1 0 )))
function Count(t, s: String): Integer;
var
Offset, P: Integer;
begin
Result := 0;
Offset := 1;
P := PosEx(t, s, Offset);
while P > 0 do
begin
Inc(Result);
P := PosEx(t, s, P + 1);
end;
end;
Strings in Pascal start at index 1, therefore Offset is set to 1.
The function counts overlapping occurrences.
The function counts overlapping occurrences.
my $t="banana bo bana bandana";
my $c =()= $t=~ m/ana/g;
print "count without overlap: $c\n";
$c =()= $t =~ m/an(?=a)/g;
print "count with overlap: $c\n";
The =()= operator makes the match operate in list context (http://www.perlmonks.org/?node_id=527973), and the (?=) operator allows it to match overlapping text (surround the part that can overlap).
count = s.count(t)
counts non-overlapping occurrences
s.scan(t).size
Overlapping occurrences will not be counted.