码迷,mamicode.com
首页 > 其他好文 > 详细

字符串strip相关函数

时间:2017-04-13 18:01:29      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:span   world   log   ash   开头   rip   pre   strip()   停止   

s.strip(rm) 删除s字符串中开头、结尾处,位于 rm删除序列的所有字符,但只要遇到非rm序列中的字符就停止
s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的所有字符,,但只要遇到非rm序列中的字符就停止
s.rstrip(rm) 删除s字符串中结尾处,位于 rm删除序列的所有字符,,但只要遇到非rm序列中的字符就停止

 当rm为空时,默认删除空白符(包括‘\n‘, ‘\r‘,  ‘\t‘,  ‘ ‘)

>>> st.rstrip()
‘\n\t hello world‘
>>> st.lstrip()
‘hello world \n\t\r‘
>>> st.strip()
‘hello world‘
>>> xml_tag = ‘<some_tag>‘
>>> xml_tag.lstrip("<")
‘some_tag>‘
>>> xml_tag.lstrip(">")
‘<some_tag>‘
>>> xml_tag.rstrip(">")
‘<some_tag‘
>>> xml_tag.rstrip("<")
‘<some_tag>‘
>>> xml_tag.strip("<").strip(">")
‘some_tag‘
>>> xml_tag.strip("<>") #删除开头和结尾的<>
‘some_tag‘
>>> gt_lt_str = "<><>gt lt str<><><>"
>>> gt_lt_str.strip("<>")
‘gt lt str‘
>>> gt_lt_str.strip("><") #删除指定序列中的字符,与排列顺序无关
‘gt lt str‘
>>> foo_str = "<foooooo>blash<foo>"
>>> foo_str.strip("<foo>")
‘blash‘
>>> foo_str.strip("foo") #虽然字符串中包含foo,但是开头遇到非删除序列的<,结尾遇到非删除序列的>,删除工作就停止了
‘<foooooo>blash<foo>‘

 

字符串strip相关函数

标签:span   world   log   ash   开头   rip   pre   strip()   停止   

原文地址:http://www.cnblogs.com/hupeng1234/p/6704979.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!