Find all the occurrences of a character in a string

后端 未结 7 576
情深已故
情深已故 2020-12-01 12:02

I am trying to find all the occurences of \"|\" in a string.

def findSectionOffsets(text):
    startingPos = 0
    endPos = len(text)

    for position in te         


        
7条回答
  •  伪装坚强ぢ
    2020-12-01 12:40

    text.find() only returns the first result, and then you need to set the new starting position based on that. So like this:

    def findSectionOffsets(text):
        startingPos = 0
    
        position = text.find("|", startingPos):
        while position > -1:
            print position
            startingPos = position + 1
            position = text.find("|", startingPos)
    

提交回复
热议问题