Finding a string multiple times in another String - Python

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

    You can do:

    >>> haystack = "abcdefabc. asdli! ndsf acba saa abe?"
    >>> needle = "abc"
    >>> for i, _ in enumerate(haystack):
    ...     if haystack[i:i + len(needle)] == needle:
    ...         print (i)
    ...
    0
    6
    

提交回复
热议问题