1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

string.v: string.count(substr)

This commit is contained in:
joe-conigliaro 2019-08-03 07:30:22 +10:00 committed by Alexander Medvednikov
parent 9e76d8a638
commit faf2f9920e

View File

@ -454,6 +454,22 @@ pub fn (s string) index_after(p string, start int) int {
return -1 return -1
} }
// counts occurrences of substr in s
pub fn (s string) count(substr string) int {
if s.len == 0 || substr.len == 0 {
return 0
}
mut n := 0
for {
i := s.index(substr)
if i == -1 {
return n
}
n++
s = s.right(i+substr.len)
}
}
pub fn (s string) contains(p string) bool { pub fn (s string) contains(p string) bool {
res := s.index(p) > 0 - 1 res := s.index(p) > 0 - 1
return res return res