这是我现在使用的代码片段:
def countOccurences(a, b)
#counts the number of occurences of a in b
count=0
cur=0
while cur < b.length do
temp=b.index(a, cur)
if temp == nil
break
end
count=count+1
cur=temp+a.length
end
return count
end
Run Code Online (Sandbox Code Playgroud)
是否有任何Ruby功能可以做到这一点?任何功能等同?还是更好的?
| 归档时间: |
|
| 查看次数: |
786 次 |
| 最近记录: |