zoukankan      html  css  js  c++  java
  • strip lstrip rstrip方法

    string.strip([chars]) 方法删除字符串开头和结尾指定的字符或字符序列(即不能删中间字符)。

    string.lstrip([chars]) 方法删除字符串开头指定的字符或字符序列(即不能删中间字符)。(l - left - strip 左侧,即开头)

    string.rstrip([chars]) 方法删除字符串结尾指定的字符或字符序列(即不能删中间字符)。(r - right - strip 右侧,即结尾)

    上面三个函数返回移除字符串string头尾指定的字符生成的新字符串,string本身不会发生改变。

    chars  指的是移除字符串头尾指定的字符序列,

              若其为空,则默认删除空白符: 、 、 、' ',即:换行、回车、制表符、空格

              若其不为空时,找出字符串string中头尾部分含有的与chars中所包含的字符相同的字符,然后将这些字符去掉

    举例说明如下:
    1. 当chars为空时,默认删除空白符

    复制代码
     1  string = " 123 456 "
     2 >>> string
     3 ' 123 456 '
     4 >>> string.strip() #删除头尾空格
     5 '123 456'
     6 >>> string.lstrip() #删除开头空格
     7 '123 456 '
     8 >>> string.rstrip()  #删除结尾空格
     9 ' 123 456'
    10 >>> string    #原字符串不变
    11 ' 123 456 '
    复制代码

    2.当chars不为空时,找出字符串string中头尾部分含有的与chars中所包含的字符相同的字符,然后将这些字符去掉

    复制代码
     1 str='12122a2b12c21212'
     2 >>> str.strip("12")   #删除头尾的1和2
     3 'a2b12c'
     4 >>> str.strip("1")    #删除头尾的1
     5 '2122a2b12c21212'
     6 >>> str.strip("2")      #删除头尾的2
     7 '12122a2b12c2121'
     8 >>> str.lstrip("12")  #删除开头的1和2
     9 'a2b12c21212'
    10 >>> str.rstrip("12")  #删除结尾的1和2
    11 '12122a2b12c'
  • 相关阅读:
    Codefroces 920F SUM and REPLACE(线段树)
    POJ 2155 Matrix (2维树状数组)
    POJ 3067 Japan (树状数组求逆序对)
    Codeforces 919D Substring (拓扑排序+树形dp)
    拓扑排序
    Codeforces 889F Letters Removing(二分 + 线段树 || 树状数组)
    线段树扫描线(2---算矩形的相交面积)
    线段树扫描线(1---算矩形的总面积)
    hdu 6168 Numbers
    Educational Codeforces Round 27 A B C
  • 原文地址:https://www.cnblogs.com/baoshilin/p/13067923.html
Copyright © 2011-2022 走看看