Find all the occurrences of a character in a string

后端 未结 7 564
情深已故
情深已故 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:52

    If text is the string that you want to count how many "|" it contains, the following line of code returns the count:

    len(text.split("|"))-1
    

    Note: This will also work for searching sub-strings.

    0 讨论(0)
提交回复
热议问题