Finding a string multiple times in another String - Python

后端 未结 7 1379
死守一世寂寞
死守一世寂寞 2021-01-22 12:00

I\'m trying to see if a string exists in another string with out using Python\'s predefined functions such as find and index..

Right now what my function takes 2 strings

相关标签:
7条回答
  • 2021-01-22 12:51

    probably the best way to do this is to keep calling the find function (this is fastest too)

    def multifind(string, value, start = 0, stop = None):
        values = []
        while True:
            found = string.find(value, start, stop)
            if found == -1:
                break
            values.append(found)
            start = found + 1
        return values
    
    print multifind('hello abc abc', 'abc')
    

    Output:

    [6, 10]
    
    0 讨论(0)
提交回复
热议问题