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代表查找索引失败。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM