查找字符串中出现的所有字符 [英] Find all the occurrences of a character in a string
本文介绍了查找字符串中出现的所有字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找到所有出现的|"在一个字符串中.
I am trying to find all the occurences of "|" in a string.
def findSectionOffsets(text):
startingPos = 0
endPos = len(text)
for position in text.find("|",startingPos, endPos):
print position
endPos = position
但我收到一个错误:
for position in text.find("|",startingPos, endPos):
TypeError: 'int' object is not iterable
推荐答案
功能:
def findOccurrences(s, ch):
return [i for i, letter in enumerate(s) if letter == ch]
findOccurrences(yourString, '|')
将返回|
出现的yourString
的索引列表.
will return a list of the indices of yourString
in which the |
occur.
这篇关于查找字符串中出现的所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文