是否有一个与myStr.find(subStr, startInd)
的类比,以得到{startInd
之前第一次出现的索引。比如从startInd
开始执行步骤-1而不是1?在
编辑
这里有一个例子:
myStr = "(I am a (cool) str)"
startInd = 9 # print(myStr[startInd]) -> "c"
print(myStr.find(")", startInd)) # -> 13
print(myStr.findBefore("(", startInd)) # -> 8
编辑二
下面的代码解决了我的问题,但并不十分方便。想问一下是否有一个简单的方法来完成这个任务
^{pr2}$
^{} 采用可选的
end
参数:因此,如果您希望
subStr
在endIndex
之前subStr
结束,可以使用myStr.find(subStr, 0, endIndex)
:如果您希望
^{pr2}$subStr
在endIndex
之前的任何地方开始,则必须使用myStr.find(subStr, 0, endIndex + len(subStr))
:相关问题 更多 >
编程相关推荐