我正在寻找像Pythons这样的函数
"foobar, bar, foo".count("foo")
显然找不到任何似乎能够做到这一点的功能。寻找单一功能或并非完全过大的功能。
我认为,现在最接近的内置对象是a的长度split
(减1)。但是要具体创建您想要的东西并不难。
我可以看到searchall
在Julia's Base中通常有用,类似于matchall
。如果您不关心实际的索引,则可以使用计数器而不是增加idxs
数组。
function searchall(s, t; overlap::Bool=false)
idxfcn = overlap ? first : last
r = findnext(s, t, firstindex(t))
idxs = typeof(r)[] # Or to only count: n = 0
while r !== nothing
push!(idxs, r) # n += 1
r = findnext(s, t, idxfcn(r) + 1)
end
idxs # return n
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句