有三个字符串,分别为 A 、B 、C,如何找出字符串C中以A开头、B结尾的子串?

这里使用一个Python字符串对象的函数:

|  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

 

 1 #_*_coding:utf-8_*_
 2 
 3 A = "world" ;
 4 B = "MaoGuy" ;
 5 
 6 C = "Hello,world.I am MaoGuy!" ;
 7 
 8 result = ""
 9 
10 if C.find(A):
11     startIndex = C.find(A)
12     if C.find(B) > startIndex:
13         endIndex = C.find(B) + len (B)
14         result = C[startIndex:endIndex]
15         
16 print (result)

 

posted @ 2017-03-16 16:18  maoguy  阅读(1712)  评论(0编辑  收藏  举报