Python已经string.find()并且string.rfind()在字符串中获取子字符串的索引.
我想知道,也许有类似的东西string.find_all()可以返回所有已创建的索引(不仅从开始或从头到尾)?
例如:
string = "test test test test"
print string.find('test') # 0
print string.rfind('test') # 15
#this is the goal
print string.find_all('test') # [0,5,10,15]
Run Code Online (Sandbox Code Playgroud) 这似乎应该是非常简单的,但我是Python的新手,并希望以最Pythonic的方式做到这一点.
我想在字符串中找到第n个子字符串.
必须有一些与我想做的事情相同的东西
mystring.find("substring", 2nd)
你怎么能用Python实现这个目标?
我想替换字符串中第n个子串的出现.
必须有一些与我想做的事情相同的东西
mystring.replace("substring", 2nd)
实现这一目标的最简单,最恐怖的方法是什么?
为什么不重复:我不想使用正则表达式这种方法,我发现的类似问题的大多数答案只是正则表达式剥离或真正复杂的功能.我真的希望尽可能简单而不是正则表达式解决方案.