美文网首页
字符串方法0x04 -- 剥离字符

字符串方法0x04 -- 剥离字符

作者: import_hello | 来源:发表于2018-11-23 00:07 被阅读0次

转载须注明出处:简书@Orca_J35 | GitHub@orca-j35

字符串不仅支持所有通用序列操作,还实现了很多附件方法。
我会以『字符串方法』为标题,分几篇笔记逐一介绍这些方法。
我会在这仓库中持续更新笔记:https://github.com/orca-j35/python_notes

strip

🔨 str.strip([chars])

Return a copy of the string with the leading and trailing characters removed. The charsargument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped:

# 如果字符串左右两侧直接包含chars中的字符,则会移除这些字符
# chars的默认值是空白符
>>> '   spacious   \t\n'.strip()
'spacious'
>>> 'www.example.com'.strip('cmowz.')
'example'

The outermost leading and trailing chars argument values are stripped from the string. Characters are removed from the leading end until reaching a string character that is not contained in the set of characters in chars. A similar action takes place on the trailing end. For example:

# 遇到不属于chars的字符便会停止剥离
>>> comment_string = '#....... Section 3.2.1 Issue #32 .......'
>>> comment_string.strip('.#! ')
'Section 3.2.1 Issue #32'

lstrip

🔨 str.lstrip([chars])

Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the charsargument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped:

>>> '   spacious   '.lstrip()
'spacious   '
>>> 'www.example.com'.lstrip('cmowz.')
'example.com'

rstrip

🔨 str.rstrip([chars])

Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the charsargument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped:

>>> '   spacious   '.rstrip()
'   spacious'
>>> 'mississippi'.rstrip('ipz')
'mississ'

相关文章

  • 字符串方法0x04 -- 剥离字符

    转载须注明出处:简书@Orca_J35 | GitHub@orca-j35 字符串不仅支持所有通用序列操作,还实现...

  • iOS - 字符串方法

    删除字符串中的空格 用系统替换字符串方法 字符串分割 字符串分割方法 一字符串是否包含另一字符串 判断方法 大写转...

  • String 常用方法汇总

    截取字符串 字符串替换 字符串拼接 Stringbuilder 方法 StringJoiner 方法 setEmp...

  • 字符串方法

    #字符串调用字符串方法修改的话,原来的字符串不变,如果用字符串方法修改了字符串,需要重新赋值 currStr ="...

  • Swift 4.0 字符串截取,拼接,字符串富文本显示

    字符串截取,调用系统方法 字符串拼接 字符串富文本 字符串截取,调用系统方法 swift 3.2 版本:

  • JavaScript 字符串

    js字符串,js字符串的概述和声明,js字符串的特性,js字符串的常用方法,js字符串的拓展方法,js字符串的案例...

  • 字符串格式化,字符串方法

    字符串格式化 方法一 方法二 字符串方法

  • Foundation框架常用方法

    NSArray方法 NSString 字符串的遍历 字符串的比较 字符串的截取和大小写 搜索字符串与替换字符串 方法列表

  • 针对ES6的新知识学习

    字符串startsWith()方法 判断字符串string是否是以str开头 字符串endsWith()方法 判断...

  • 12-27字符串内建函数

    下面所有字符串相关方法的使用方式都是: 字符串.方法名() 1.字符串1 . endswith(字符串2) -...

网友评论

      本文标题:字符串方法0x04 -- 剥离字符

      本文链接:https://www.haomeiwen.com/subject/sylqqqtx.html