Python在索引后找到第一个出现的字符

Gun*_*her 17 python

我试图获取指定索引的字符串中出现的第一个字符的索引.例如:

string = 'This + is + a + string'

# The 'i' in 'is' is at the 7th index, find the next occurrence of '+'
string.find_after_index(7, '+')

# Return 10, the index of the next '+' character
>>> 10
Run Code Online (Sandbox Code Playgroud)

Chr*_*nds 21

Python是如此可预测:

>>> string = 'This + is + a + string'
>>> string.find('+',7)
10
Run Code Online (Sandbox Code Playgroud)

结帐help(str.find):

find(...)
    S.find(sub[, start[, end]]) -> int

    Return the lowest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.
Run Code Online (Sandbox Code Playgroud)

也可以使用,str.index除了这将raise ValueError代替-1未找到子字符串.