Python 的字符串类子串查找函数

Python 的字符串类有个很好用的函数,可很方便的用于与查找Python字符串类型对象子串相关的操作,具体的API如下:

|  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

假定S为string类型的对象,调用find函数,将子串或子串的子串(也就是子串从start到end的切片)作为参数传入。

返回值为int类型,若S包含有传入参数sub,则返回子串(substring)在父串(S)中的索引,若不包含作为参数传入的子串,则返回-1代表查找索引失败。

posted @ 2017-03-20 21:35  maoguy  阅读(8366)  评论(0编辑  收藏  举报