当前位置:Gxlcms > Python > python根据开头和结尾字符串获取中间字符串的方法

python根据开头和结尾字符串获取中间字符串的方法

时间:2021-07-01 10:21:17 帮助过:103人阅读

本文实例讲述了python根据开头和结尾字符串获取中间字符串的方法。分享给大家供大家参考。具体分析如下:

这里给定一个字符串,指定开头和结尾的字符串,返回中间包夹的字符串,比如:
content:

bitsCN.com

startStr:

endStr:

返回结果:bitsCN.com

  1. def GetMiddleStr(content,startStr,endStr):
  2. startIndex = content.index(startStr)
  3. if startIndex>=0:
  4. startIndex += len(startStr)
  5. endIndex = content.index(endStr)
  6. return content[startIndex:endIndex]
  7. if __name__=='__main__':
  8. print(GetMiddleStr('<div class="a">bitsCN.com</div>','<div class="a">','</div>'))

希望本文所述对大家的Python程序设计有所帮助。

人气教程排行