Number of occurrences of a substring in a string

前端 未结 6 1557
死守一世寂寞
死守一世寂寞 2020-12-12 04:08

I need to count the nunber of times the substring \'bob\' occurs in a string.

Example problem: Find the number of times \'bob\' occurs in string s such

6条回答
  •  暖寄归人
    2020-12-12 05:07

    def count_substring(string, sub_string):
    count=a=0
    while True:
        a=string.find(sub_string)
        string=string[a+1:]
        if a>=0:
            count=count+1;
        else:
            break
    return count
    

提交回复
热议问题