计算字符串中子字符串出现的次数 - 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章