zoukankan      html  css  js  c++  java
  • python replace 方法

    replace 方法

    返回根据正则表达式进行文字替换后的字符串的复制。stringObj.replace(rgExp, replaceText)

    参数
    stringObj
    必选项。要执行该替换的 String 对象或字符串文字。该字符串不会被 replace 方法修改。

    rgExp
    必选项。为包含正则表达式模式或可用标志的正则表达式对象。也可以是 String 对象或文字。如果 rgExp 不是正则表达式对象,它将被转换为字符串,并进行精确的查找;不要尝试将字符串转化为正则表达式。

    replaceText必选项。是一个String 对象或字符串文字,对于stringObj 中每个匹配 rgExp 中的位置都用该对象所包含的文字加以替换。在 Jscript 5.5 或更新版本中,replaceText 参数也可以是返回替换文本的函数。

    例如:

    content='(please) input your name:'

    con=content.replace('(','')

    print con |||| please) input your name


    content.replace(' ',' ').replace(' ',' ').replace('\','\\').replace(',','\,')

    ---------------------

    本文来自 MissZhang_ 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u013052394/article/details/74002265?utm_source=copy
    ==================================================================================
    描述
    Python replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
    语法
    replace()方法语法:
    str.replace(old, new[, max])
    参数
    old -- 将被替换的子字符串。
    new -- 新字符串,用于替换old子字符串。
    max -- 可选字符串, 替换不超过 max 次
    返回值
    返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。
    实例
    以下实例展示了replace()函数的使用方法:
    #!/usr/bin/python

    str = "this is string example....wow!!! this is really string";
    print str.replace("is", "was");
    print str.replace("is", "was", 3);
    以上实例输出结果如下:
    thwas was string example....wow!!! thwas was really string
    thwas was string example....wow!!! thwas is really string

  • 相关阅读:
    ssd 的anchor生成详解
    Qt小技巧8.利用反射机制通过类名创建Qt对象
    项目经验2.需求才是王道
    Qt实战12.可自由展开的ToolBox
    Qt实战11.进程窗口集成之假装写了个第三方软件
    Qt小技巧7.Qt4集成fusion风格
    Qt杂谈3.快速体验Qt for Android(windows平台)
    Qt实战10.支持最小化和最大化的QDockWidget
    gitlab 拉取远程分支代码
    CentOS7下用jdk1.7编译hadoop-2.7.1全过程详解
  • 原文地址:https://www.cnblogs.com/duanlinxiao/p/9820816.html
Copyright © 2011-2022 走看看