有三个字符串,分别为 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
| 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)
相关文章推荐
- python 4-2 如何判断字符串a是否以字符串b开头或结尾str.startswith/os.stat(filename)/stat.S_IRWXU
- 计算两个字符串的最长公共子串,找两个字符串的公共部分,如何在两个字符串中找出相同的字符串
- python_如何判断字符串a以某个字符串开头或结尾?
- c语言初学--计算字符串中数字的最大长度并提取 以及输入两个子串 提取分别以这为开头和结尾的字符串
- Python高级编程-如何判断字符串a是否是以字符串b开头或结尾?
- python-12-如何判断字符串a是否以字符串b开头或结尾(改变文件权限)?
- 如何去掉字符串开头的0和结尾的0?
- 【慕课网】如何判断字符串a是否以字符串b开头或结尾 给某文件系统所有.sh文件和.py文件加上可执行权限
- 每天学习一算法系列(30)(给一个很长的字符串str 还有一个字符集比如{a,b,c} 找出str 里包含{a,b,c}的最短子串。要求O(n).)
- tyvj 1170 0/1字符串问题 字符串中仅包含0和1两个字符;②字符串的长度为n;③字符串中不含有三个连续的相同子串 搜索
- 找出两个字符串的最大公共子串
- JavaScript/JQuery去掉字符串开头与结尾的空格
- 如何让以特定字符串结尾的所有站点都被加入到Local Intranet Zone中?
- 找出字符串的最长不重复子串
- 常考题:正则表达式 寻找所有以某个字符串开头以某个字符串结尾的所有中间字符串
- 如何将一个字符串中的数据 以逗号为标示 分别存到另一个数组中
- Interview Q&A - 房间里有三盏灯,屋里有三个开关分别控制这三盏灯。如何只进入房间一次就知道哪一个开关控制哪一盏灯?
- 找出一个字符串中最长连续相同子串
- Javascript contians 函数 字符串包含函数 及 删除字符串开头和结尾空格的方法
- Linux系统中,有两个文件file1和file2,每个文件的每一行都是#UUID,其中的每一UUID表示一个号。要找出在file1中有而在file2中没有的UUID,使用cat,sort,uniq三个命令如何实现