您的位置:首页 > 其它

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

2017-03-16 16:18 344 查看
这里使用一个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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐