計算字符串中子字符串出現的次數 - Count number of occurrences of a substring in a string

問題:

How can I count the number of times a given substring is present within a string in Python?如何計算給定子字符串出現在 Python 字符串中的次數?

For example:例如:

>>> 'foo bar foo'.numberOfOccurrences('foo')
2

解決方案:

參考一: https://stackoom.com/question/bLGr
參考二: Count number of occurrences of a substring in a string
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章