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