查找字符串中两个字符中第一个字符的索引

2024-05-05 19:07:37 发布

您现在位置:Python中文网/ 问答频道 /正文

给定以下字符串:

foo = "hello + there"
bar = "hello - there"
baz = "one + two - three"
fuz = "one - two + three"

是否有(相对)直接的方法来获取第一个字符的索引,即+-

foo.index_multiple('+-') # 6
bar.index_multiple('+-') # 6
baz.index_multiple('+-') # 4
fuz.index_multiple('+-') # 4

编辑:我不要求定制代码(例如Python例程)——这相对简单,我可以自己做。我只想知道是否有内置的用于此目的的东西,可以使用正则表达式,也可以不使用正则表达式


Tags: 方法字符串编辑helloindexfoobarbaz