Finding a string multiple times in another String - Python

后端 未结 7 1375
死守一世寂寞
死守一世寂寞 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:42

    def multi_find(s, r):
    
        s_len = len(s)
        r_len = len(r)
    
        _complete = []
    
        if s_len < r_len:
            n = -1
        else:
    
            for i in xrange(s_len):
                # search for r in s until not enough characters are left
                if s[i:i + r_len] == r:
                    _complete.append(i)
                else:
                    i = i + 1
        print(_complete)
    
    multi_find("abcdefabc. asdli! ndsf abc saa abe?", "abc")
    

提交回复
热议问题